private void Button_Click(object sender, RoutedEventArgs e) { // ONE OF THE CELL BUTTONS WAS CLICKED // SEE IF THE GAME IS ALREADY OVER ... if (GameOver == true) { uxTurn.Text = "Game is over. Start a new game from the Menu."; return; } // FIND OUT WHICH BUTTON WAS CLICKED BY EVALUATING THE TAG FIELD Button _Sender = (Button)sender; string _ButtonID_Tag = _Sender.Tag.ToString(); string _Index = _ButtonID_Tag.Remove(_ButtonID_Tag.IndexOf(','), 1); // strip the , character then use result as index into dictionary if (_GameBoard[_Index] == BoardMarker.Blank) // CLICKED ON A BLANK SPOT ON THE BOARD { // PUT THE MARKER OF THE CURRENT PLAYER ONTO THE BOARD if (CurrentTurnMarker == BoardMarker.Mark_X) { _Sender.Content = "X"; } else { _Sender.Content = "O"; } _GameBoard[_Index] = CurrentTurnMarker; // CHECK TO SEE IF THERE IS A WINNER if (((CheckRow(CurrentTurnMarker) == true) || (CheckCol(CurrentTurnMarker) == true) || (CheckDiag(CurrentTurnMarker) == true))) { uxTurn.Text += " WINNER !! WINNER !! "; GameOver = true; return; } // SEE IF THERE ARE ANY BLANKS LEFT ... IF NO BLANKS LEFT THEN GAME IS OVER AND THERE WAS NO WINNER if (CheckBlankSpace() == false) { GameOver = true; uxTurn.Text = "Game Over. Draw."; return; } // MOVE FINISEHD ... NOW CHANGE TO THE NEXT PLAYER AND CYCLE if (CurrentTurnMarker == BoardMarker.Mark_X) { CurrentTurnMarker = BoardMarker.Mark_O; uxTurn.Text = "It is now Player O's turn."; } else { CurrentTurnMarker = BoardMarker.Mark_X; uxTurn.Text = "It is now Player X's turn."; } } }
private bool CheckDiag(BoardMarker _Marker) { // CHECK THE DIAGONALS TO SEE IF THERE IS THREE IN A ROW FOR THE INDICATED MARKER TYPE if (((_GameBoard["00"] == _Marker) && (_GameBoard["11"] == _Marker) && (_GameBoard["22"] == _Marker)) || ((_GameBoard["20"] == _Marker) && (_GameBoard["11"] == _Marker) && (_GameBoard["02"] == _Marker))) { return(true); } else { return(false); } }
private bool CheckRow(BoardMarker _Marker) { // CHECK THE ROWS TO SEE IF THERE IS THREE IN A ROW FOR THE INDICATED MARKER TYPE for (int _Row = 0; _Row <= 2; _Row++) { string _Index = _Row.ToString(); if ((_GameBoard[_Index + "0"] == _Marker) && (_GameBoard[_Index + "1"] == _Marker) && (_GameBoard[_Index + "2"] == _Marker)) { return(true); } } return(false); }
private bool CheckCol(BoardMarker _Marker) { // CHECK THE COLUMNS TO SEE IF THERE IS THREE IN A COL FOR THE INDICATED MARKER TYPE for (int _Col = 0; _Col <= 2; _Col++) { string _Index = _Col.ToString(); if ((_GameBoard["0" + _Index] == _Marker) && (_GameBoard["1" + _Index] == _Marker) && (_GameBoard["2" + _Index] == _Marker)) { return(true); } } return(false); }
/* Initializes markers (A, B, C, D...) */ private void InitializeMarkers() { for (int i = 0; i < 8; i++) { // Vertical markers CheckersCoordinateSet position = new CheckersCoordinateSet( new Point(2, i * cellSize.Height + (cellSize.Height / 2) - (cellSize.Height / 6)), new Size(12, 12), new Point(0, i)); markers[0, i] = new BoardMarker(Convert.ToChar('8' - i), position); Game.drawingController.AddToDrawingList(markers[0, i]); // Horizontal markers position = new CheckersCoordinateSet( new Point(i * cellSize.Width + (cellSize.Width / 2) - (cellSize.Width / 6), 8 * cellSize.Height - 18), new Size(12, 12), new Point(i, 0)); markers[1, i] = new BoardMarker(Convert.ToChar('A' + i), position); Game.drawingController.AddToDrawingList(markers[1, i]); } }