private void gameTimer_Tick(object sender, EventArgs e) { counter++; gameTimerLabel.Text = (10 - counter) + " seconds remaining..."; if (!playersTurn && counter >= currAiTimeout) { stopTimer(); Board b = new Board(board, boardWinLen); //Hi, I'm a tic-tac-ro-bot int[] ai = Ttt.getAiMove(b); //robot dies, what a pity if (ai != null) { if ((crossFirst && playerStarts) || (!crossFirst && !playerStarts)) { updatePictureBox(board[ai[0], ai[1]], 'o'); } else { updatePictureBox(board[ai[0], ai[1]], 'x'); } } //warning, this funny game may soon end! if (performEndOfTurnChecks(b)) { startTimer(); playersTurn = true; } } else if (playersTurn && counter >= 10) { stopTimer(); playersTurn = false; performEndOfTurnChecks(null); } }
private bool performEndOfTurnChecks(Board b) { if (b == null || Ttt.gameOver(b)) { char winner = ' '; if (b != null) { winner = Ttt.winner(b); } else { if ((crossFirst && playerStarts) || (!crossFirst && !playerStarts)) { winner = 'o'; } else { winner = 'x'; } } string msg, shortmsg; if (winner == ' ') { msg = "What a duel! However, it ended with a draw."; shortmsg = "draw"; } else if ((crossFirst && (winner == 'x' && playerStarts || winner == 'o' && !playerStarts)) || (!crossFirst && (winner == 'x' && !playerStarts || winner == 'o' && playerStarts))) { msg = "Congratz to the true mastermind, you've beaten the pseudo-random number generator" + " implemented by Microsoft! Awesome indeed!"; if ((crossFirst && playerStarts) || (!crossFirst && !playerStarts)) { shortmsg = "x (player) won"; } else { shortmsg = "o (player) won"; } } else { msg = "It seems that you have lost."; if ((crossFirst && playerStarts) || (!crossFirst && !playerStarts)) { shortmsg = "o (AI) won"; } else { shortmsg = "x (AI) won"; } } statusLabel.Text = "Game over, " + shortmsg + "."; TreeNode n = gameHistoryTree.Nodes[0]; TreeNode node = n.Nodes[n.Nodes.Count - 1]; n.Expand(); node.Nodes.Add(shortmsg); node.Expand(); MessageBox.Show(this, msg, "Game over", MessageBoxButtons.OK, MessageBoxIcon.Information); playersTurn = false; return(false); } else { //staying alive... statusLabel.Text = "Your move!"; playersTurn = true; return(true); } }