Classe para Jogo da velha em console app
Por: Alexandre Ubaldo • 26/8/2015 • Trabalho acadêmico • 3.725 Palavras (15 Páginas) • 304 Visualizações
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game
{
public class Jogo
{
private int margin = 15;
private int[] columnsPosition = { 24, 40, 56 };
private int[] rowsPosition = { 2, 6, 9 };
private int currentXPos, currentYPos;
private int markedSquares;
private List<Square> squares;
private bool jogoFinalizado = false;
public Jogo()
{
markedSquares = 0;
currentXPos = 0;
currentYPos = 0;
Welcome();
printGrid();
Console.SetCursorPosition(columnsPosition[currentXPos], rowsPosition[currentYPos]);
}
private static void Welcome()
{
Console.WriteLine(string.Format(@"
_ _ __ __ _ _
| | | | \ \ / / | | |
| | ___ __ _ ___ __| | __ _ \ \ / /__| | |__ __ _
_ | |/ _ \ / _` |/ _ \ / _` |/ _` | \ \/ / _ \ | '_ \ / _` |
| |__| | (_) | (_| | (_) | | (_| | (_| | \ / __/ | | | | (_| |
\____/ \___/ \__, |\___/ \__,_|\__,_| \/ \___|_|_| |_|\__,_| v:0.1b
__/ |
|___/ by Alexandre Ubaldo"));
Console.ReadKey();
Console.Clear();
}
public void CheckWin(int player)
{
bool won = false;
if(squares.Where(s => (s.player == player && s.xPos == currentXPos)).Count() >= 3)
{
won = true;
}
if (squares.Where(s => (s.player == player && s.yPos == currentYPos)).Count() >= 3)
{
won = true;
}
if (squares.Where(s => (s.player == player && s.id == 1)).Count() + squares.Where(s => (s.player == player && s.id == 5)).Count() + squares.Where(s => (s.player == player && s.id == 9)).Count() >= 3 || squares.Where(s => (s.player == player && s.id == 3)).Count() + squares.Where(s => (s.player == player && s.id == 5)).Count() + squares.Where(s => (s.player == player && s.id == 7)).Count() >= 3)
{
won = true;
}
if (won) {
WriteMsg(string.Format("Yei, Player {0} ganhou!", player));
Console.ReadKey();
jogoFinalizado = true;
}
}
public void Start()
{
initSquares();
ConsoleKeyInfo keyInfo;
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape && !jogoFinalizado)
{
keyDetection(keyInfo);
}
}
public void initSquares()
{
squares = new List<Square>();
squares.Add(new Square { id = 1, player = 0, xPos = 0, yPos = 0, active = true });
squares.Add(new Square { id = 2, player = 0, xPos = 1, yPos = 0, active = false });
squares.Add(new Square { id = 3, player = 0, xPos = 2, yPos = 0, active = false });
squares.Add(new Square { id = 4, player = 0, xPos = 0, yPos = 1, active = false });
squares.Add(new Square { id = 5, player = 0, xPos = 1, yPos = 1, active = false });
squares.Add(new Square { id = 6, player = 0, xPos = 2, yPos = 1, active = false });
squares.Add(new Square { id = 7, player = 0, xPos = 0, yPos = 2, active = false });
squares.Add(new Square { id = 8, player = 0, xPos = 1, yPos = 2, active = false });
squares.Add(new Square { id = 9, player
...