static void aTurn(int turn, Board gb, Player playerOne, Player playerTwo) { bool again = true; do { askPlayerForInput(gb, playerOne); gb.play(ui.selectedColumn, ui.selectedRow, 1); if ((turn > 2) && (gb.checkForVictory())) { ui.announceWinner(playerOne); again = false; } else if ((turn > 4) && (!gb.checkForVictory())) { ui.announceDraw(playerOne, playerTwo); again = false; } askPlayerForInput(gb, playerTwo); gb.play(ui.selectedColumn, ui.selectedRow, 2); if ((turn > 2) && (gb.checkForVictory())) { ui.announceWinner(playerTwo); again = false; } turn++; } while (again); }
static void askPlayerForInput(Board gb, Player pl) { ConsoleKeyInfo input; do { input = ui.askPlayerForInput(pl); if (input.Key == ConsoleKey.UpArrow) { ui.selectedRow = ui.selectedRow-1; } if (input.Key == ConsoleKey.DownArrow) { ui.selectedRow = ui.selectedRow+1; } if (input.Key == ConsoleKey.LeftArrow) { ui.selectedColumn = ui.selectedColumn-1; } if (input.Key == ConsoleKey.RightArrow) { ui.selectedColumn = ui.selectedColumn+1; } ui.selectedRow = correctOutOfBounds(ui.selectedRow, gb.boardSize); ui.selectedColumn = correctOutOfBounds(ui.selectedColumn, gb.boardSize); ui.clearScreen(); ui.drawBoard(gb); } while (input.Key != ConsoleKey.Enter); }
public static void drawBoard(Board gameboard) { drawHeader(); for (int y = 0; y < gameboard.boardSize; y++) { for (int x = 0; x < gameboard.boardSize; x++) { char symbol = gameboard.getSquare(x,y); if (x == 0) Console.Write(" " + symbol.ToString() + " |"); else if (x == gameboard.boardSize -1) Console.Write("| " + symbol.ToString() + " "); else Console.Write(" " + symbol.ToString() + " "); } if (y == selectedRow) { Console.Write( " < " ); } Console.WriteLine( ); if (y != gameboard.boardSize - 1) Console.WriteLine("-----------"); } Console.WriteLine(); for (int x = 0; x < gameboard.boardSize; x++) { if (x == selectedColumn) { Console.Write(" ^ "); } else { Console.Write(" "); } } Console.WriteLine(); }
static void Main(string[] args) { Player playerOne; Player playerTwo; Board gb; int turn = 0; bool anotherGame = true; do { ui.clearScreen(); playerOne = new Player(ui.getPlayersName("Player one"), 1); playerTwo = new Player(ui.getPlayersName("Player two"), 2); gb = new Board(3); ui.clearScreen(); ui.drawBoard(gb); turn++; aTurn(turn, gb, playerOne, playerTwo); anotherGame = ui.askForAnotherGame(); } while (anotherGame); }