Constant.GameState IYatttgFacade.MakeMove(Player player, int position) { // To match up with internal array range. position--; // Update the grid cm_.SetCellAtIndex(position, player.Marker); currentGameState_ = GameLogic.CheckGridForWinner(cm_, player.Marker, currentGameState_); return currentGameState_; }
Constant.GameState IYatttgFacade.InitGame(Player p1, Player p2) { p1_ = p1; p2_ = p2; // Flip a coin on who should start first. p1Turn = RandomFirstTurn(); // Init grid cm_.InitGrid(); // Game is now running. currentGameState_ = Constant.GameState.InProgress; return currentGameState_; }
public YatttgModel() { // Initialize default values. p1_ = null; p2_ = null; // Give Nort and Cross default values. Nort = new Nort(); Cross = new Cross(); cm_ = new CellManager(); // Initial state is to get player information. currentGameState_ = Constant.GameState.PlayerInfo; }
static void Main(string[] args) { Player p1, p2; bool playing = true; // Init the Yatttg facade IYatttgFacade yatttg = new YatttgModel.YatttgModel(); Console.WriteLine("Welcome to Yatttg! (Yet another tic-tac-toe game)."); Console.WriteLine("The current characters for the norts and crosses are {0} and {1} respectively.", yatttg.Nort.Character, yatttg.Cross.Character); // Users can change the characters for the norts and crosses, if they wish. HandleChangeChars(yatttg); // Set up each player Console.Write("Enter player name for norts: "); string name = Console.ReadLine(); p1 = new Player(name, yatttg.Nort); Console.Write("Enter player name for crosses: "); name = Console.ReadLine(); p2 = new Player(name, yatttg.Cross); // Start the game while (playing) { Tuple<Player, Constant.GameState> game = PlayGame(yatttg, p1, p2); bool reprompt = true; if (game.Item2 == Constant.GameState.Win) Console.WriteLine("{0} Wins!", game.Item1.Name); else if (game.Item2 == Constant.GameState.Tie) Console.WriteLine("{0} tied the game!", game.Item1.Name); while (reprompt) { Console.Write("Play again (y/N)? "); char choice = GetChar(); if (choice == 'y') break; // Reprompt if the choice wasn't n or empty reprompt = ((choice != 'n') && (choice != '\0')); } if (!reprompt) playing = false; } }
static Tuple<Player, Constant.GameState> PlayGame(IYatttgFacade yatttg, Player p1, Player p2) { Constant.GameState currentGameState_; currentGameState_ = yatttg.InitGame(p1, p2); Player turn = null; bool error = false; while (currentGameState_ == Constant.GameState.InProgress) { Console.Clear(); if (!error) turn = yatttg.StartNextTurn(); Console.WriteLine("{0}'s Turn ({1}):", turn.Name, turn.Marker.Character); Console.WriteLine(); DisplayGrid(yatttg); Console.WriteLine("Press the number of the cell you would like to select."); try { int option = Convert.ToInt32(Console.ReadKey(true).KeyChar.ToString()); currentGameState_ = yatttg.MakeMove(turn, option); error = false; } catch (Exception ex) { Console.WriteLine("An error occured."); Console.WriteLine(ex.Message); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); error = true; } } return new Tuple<Player, Constant.GameState>(turn, currentGameState_); }