/// <summary> /// Creates a new Game that uses the specified random number generator. /// </summary> public Game(Rand rand) { if(rand == null) throw new ArgumentNullException("rand"); _rand = rand; _deck = new Deck(_rand); _board = new Board(); InitializeBoard(); _prevBoard = new Board(_board); _tempBoard = new Board(); }
/// <summary> /// Creates a new Board copied from the specified Board. /// </summary> public Board(Board copyFrom) { CopyFrom(copyFrom); }
/// <summary> /// Copies the state of this Board from the specified Board. /// </summary> public void CopyFrom(Board board) { if(board == null) throw new ArgumentNullException("board"); Array.Copy(board._cards, _cards, _cards.Length); }
/// <summary> /// Main application entry point. /// </summary> private static void Main() { // Build the board and initialize the deck. Deck deck = new Deck(new Rand()); Board board = new Board(); Console.WriteLine("Let's initialize the board..."); Console.WriteLine("The format for each line should be four characters, each a 1, 2, 3, or any other character to represent an empty space."); for(int y = 0; y < board.Height; y++) { Console.Write("Enter row {0}: ", y); string rowStr = Console.ReadLine(); if(rowStr.Length != board.Width) { Console.WriteLine("Invalid length of entered row."); y--; continue; } for(int x = 0; x < board.Width; x++) { Card card = GetCardFromChar(rowStr[x], false); if(card != null) { board[x, y] = card; deck.RemoveCard(card.Value); } } } Console.WriteLine("Board and deck successfully initialized."); Stack<Board> boardsStack = new Stack<Board>(); Stack<Deck> decksStack = new Stack<Deck>(); // Now let's play! while(true) { redo: // Print the current board status. Console.WriteLine("--------------------"); for(int y = 0; y < board.Height; y++) { for(int x = 0; x < board.Width; x++) { Card c = board[x, y]; if(c != null) Console.Write("{0},", c.Value); else Console.Write(" ,"); } Console.WriteLine(); } Console.WriteLine("--------------------"); Console.WriteLine("Current total score: {0}", board.GetTotalScore()); // Get the next card. Console.Write("What is the next card? "); string nextCardStr; Card nextCard; do { nextCardStr = Console.ReadLine(); if(nextCardStr == "undo") { board = boardsStack.Pop(); deck = decksStack.Pop(); goto redo; } } while(nextCardStr.Length != 1 || (nextCard = GetCardFromChar(nextCardStr[0], true)) == null); NextCardHint nextCardHint = GetNextCardHint(nextCard); // Choose a move. Console.Write("Thinking..."); ShiftDirection? aiDir = _bot.GetNextMove(new FastBoard(board), new FastDeck(deck), nextCardHint); if(aiDir != null) Console.WriteLine("\nSWIPE {0}.", aiDir.Value.ToString().ToUpper()); else { Console.WriteLine("NO MORE MOVES."); break; } // Confirm the swipe. ShiftDirection? actualDir = aiDir.Value; /*do { Console.Write("What direction did you swipe in? (l, r, u, d, or just hit enter for the suggested swipe) "); string dirStr = Console.ReadLine(); actualDir = GetShiftDirection(dirStr, aiDir.Value); } while(actualDir == null);*/ List<IntVector2D> newCardCells = new List<IntVector2D>(); board.Shift(actualDir.Value, newCardCells); // Get the new card location. int newCardIndex; if(newCardCells.Count > 1) { Console.WriteLine("Here are the locations where a new card might have been inserted:"); for(int y = 0; y < board.Height; y++) { for(int x = 0; x < board.Width; x++) { int index = newCardCells.IndexOf(new IntVector2D(x, y)); if(index >= 0) Console.Write((char)('a' + index)); else Console.Write('.'); } Console.WriteLine(); } Console.Write("Where was it actually inserted? "); do { string indexStr = Console.ReadLine(); if(indexStr.Length == 1) newCardIndex = indexStr[0] - 'a'; else newCardIndex = -1; } while(newCardIndex < 0 || newCardIndex >= newCardCells.Count); } else { newCardIndex = 0; } // Get new card value. int newCardValue; if(nextCardHint == NextCardHint.Bonus) { do { Console.Write("!!! What is the value of the new card? "); } while(!TryGetNewCardValue(Console.ReadLine(), out newCardValue)); } else { newCardValue = (int)nextCardHint + 1; } deck.RemoveCard(newCardValue); board[newCardCells[newCardIndex]] = new Card(newCardValue, -1); boardsStack.Push(new Board(board)); decksStack.Push(new Deck(deck)); } Console.WriteLine("FINAL SCORE IS {0}.", board.GetTotalScore()); }