public static bool addMove(BoardMovePosition _position, BoardMove _boardMove) { if (_board[_position.X, _position.Y] == BoardMove.Empty && (_position.X != -1 || _position.Y != -1)) { _board[_position.X, _position.Y] = _boardMove; if (_boardMove == BoardMove.X) { playerOneMoves.Add(_position); } else if (_boardMove == BoardMove.O) { playerTwoMoves.Add(_position); } TicTacToeBoard.cheackWinner(); return(true); } return(false); }
private static void runGame() { int tempX, tempY; bool flag = true; TicTacToeBoard.drawBoard(); while (true) { if (TicTacToeBoard._playerOne.isWiner || TicTacToeBoard._playerTwo.isWiner) { break; } while (flag) { Console.WriteLine(TicTacToeBoard._playerOne.Name + "'s move (1 to 3) ,"); try { Console.Write("Y axis value : "); tempX = Convert.ToInt32(Console.ReadLine()) - 1; Console.Write("X axis value : "); tempY = Convert.ToInt32(Console.ReadLine()) - 1; if (TicTacToeBoard.addMove(new BoardMovePosition(tempX, tempY), BoardMove.X)) { flag = false; } else { Console.WriteLine("wrong data format"); } } catch (Exception e) { Console.WriteLine(e.Message.ToString()); } } TicTacToeBoard.drawBoard(); flag = true; if (TicTacToeBoard._playerOne.isWiner || TicTacToeBoard._playerTwo.isWiner) { break; } while (flag) { Console.WriteLine(TicTacToeBoard._playerTwo.Name + "'s move(1 to 3) ,"); try { Console.Write("Y axis value : "); tempX = Convert.ToInt32(Console.ReadLine()) - 1; Console.Write("X axis value : "); tempY = Convert.ToInt32(Console.ReadLine()) - 1; if (TicTacToeBoard.addMove(new BoardMovePosition(tempX, tempY), BoardMove.O)) { flag = false; } else { Console.WriteLine("wrong data format"); } } catch (Exception e) { Console.WriteLine(e.Message.ToString()); } } TicTacToeBoard.drawBoard(); flag = true; } Console.WriteLine( "Game ends, winner is : " + ((TicTacToeBoard._playerOne.isWiner && !TicTacToeBoard._playerTwo.isWiner) ? TicTacToeBoard._playerOne.Name : ((TicTacToeBoard._playerTwo.isWiner && !TicTacToeBoard._playerOne.isWiner) ? TicTacToeBoard._playerTwo.Name : "match draw"))); Console.WriteLine( "score => " + TicTacToeBoard._playerOne.Name + " : " + TicTacToeBoard._playerOne.leaderBoard.ToString() + " " + TicTacToeBoard._playerTwo.Name.ToString() + " : " + TicTacToeBoard._playerTwo.leaderBoard.ToString()); }