Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Maurice Harris

Member Since 15 Nov 2012
Offline Last Active Nov 19 2012 11:02 PM
-----

Posts I've Made

In Topic: Completed Tic Tac Toe - Critical Critiques and Advice

19 November 2012 - 04:55 AM

I figured out how to generalize my win checking like you said Trienco. Lately I've been trying to make it work so if for example I had a 9x6 board it would check for n in a row every time someone made a move, however I find that I end up repeating myself, my code becomes bloated, and I haven't figured out how to check for diagonal wins where x != y.

I've changed the board from a 1D char array to a 2D int array so I could better visualize logic:
[source lang="csharp"]using System;namespace TicTacToeTests{ class Program { const int BOARD_WIDTH = 9; const int BOARD_HEIGHT = 6; static int turnCount = 0; // Is going to be used to determine when the game is at a tie static int[,] board = new int[BOARD_WIDTH, BOARD_HEIGHT]; static void Move(int x, int y, int player) { // Number of spots in a row to win int n = 4; // Check to see if the spot is empty if(board[x,y] == 0) { board[x,y] = player; } turnCount++; int i = 0; int xMatch = 0; // Check Column while (i < (BOARD_WIDTH)) { if (board[i, y] != player) { xMatch = 0; } else { xMatch++; if (xMatch >= n) { Console.WriteLine("\nPlayer " + player + " wins!"); break; } } i++; } int j = 0; int yMatch = 0; // Check Row while (j < (BOARD_HEIGHT)) { if (board[x, j] != player) { yMatch = 0; } else { yMatch++; if (yMatch >= n) { Console.WriteLine("\nPlayer " + player + " wins!"); break; } } j++; } } static void Main(string[] args) { board[2, 4] = 1; board[2, 3] = 1; Move(2, 2, 1); Move(2, 5, 1); } }}[/source]

In Topic: Completed Tic Tac Toe - Critical Critiques and Advice

16 November 2012 - 07:45 PM

Great, I'll go back to reading for now. Thank you all for giving me advice and help, I hope to share better projects in the future.

In Topic: Completed Tic Tac Toe - Critical Critiques and Advice

16 November 2012 - 07:01 PM

I made the suggestions you gave to me and my program feels and looks less bloated. I feel once I get further along in reading The C# Yellow Book by Robert Miles, I'll be able to improve upon my design little by little until I'm satisfied. I just recently got to the 4th chapter of the book and it's starting to talk about objects and how to represent them so hopefully I'll be able to come back very soon and further improve the design of this and future projects. Trienco, I've been thinking about what you said about having a board that's 4x4, 5x5, or any random 2 values and I'm having trouble thinking about how I could handle this without thinking myself into a corner.

Updated Source:
[source lang="csharp"]/** Tic Tac Toe - by Maurice Harris* Last Modified: 11/16/12*/using System;namespace TicTacToeGame{ class TicTacToe { // The Tic Tac Toe board static char[] board = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }; // Used to keep track of whether the current game is over static bool gameOver = false; // Used to keep track of whether a new game has started static bool newGame = true; // Draws a tic tac toe board static void DrawBoard() { Console.WriteLine("\n\n " + board[6] + " | " + board[7] + " | " + board[8]); Console.WriteLine(" ---+---+---"); Console.WriteLine(" " + board[3] + " | " + board[4] + " | " + board[5]); Console.WriteLine(" ---+---+---"); Console.WriteLine(" " + board[0] + " | " + board[1] + " | " + board[2] + "\n\n"); } // Clears a tic tac toe board static void ClearBoard() { // Loop through char array elements and set them equal to ' ' for (int i = 0; i < board.Length; i++) { board[i] = ' '; } } // Checks the board to see if the space is taken up by an O or X static bool IsSpaceFree(int move) { return board[move] == ' '; } // Marks spot on the tic tac toe board with a marker if the space is free static bool MakeMove(char marker, int move) { if (IsSpaceFree(move)) { board[move] = marker; return true; } else { Console.WriteLine("\nThat spot is taken."); return false; } } // Take input from either the numpad or numerical keys 1-9 and mark the board static void TakeInput(char marker) { // Keep track of whether or not the user has pressed a valid key for marking the board bool validKeyPressed = false; // Keep track of the key pressed by the user ConsoleKeyInfo keyPressed; // The spot picked by the user int spot = -1; // While loop to catch valid input from the user while (!validKeyPressed) { keyPressed = Console.ReadKey(true); // If the key pressed is valid and the spot is unmarked, mark it, // Otherwise break the switch statement and take new key input switch (keyPressed.Key) { case ConsoleKey.NumPad1: case ConsoleKey.D1: spot = 0; break; case ConsoleKey.NumPad2: case ConsoleKey.D2: spot = 1; break; case ConsoleKey.NumPad3: case ConsoleKey.D3: spot = 2; break; case ConsoleKey.NumPad4: case ConsoleKey.D4: spot = 3; break; case ConsoleKey.NumPad5: case ConsoleKey.D5: spot = 4; break; case ConsoleKey.NumPad6: case ConsoleKey.D6: spot = 5; break; case ConsoleKey.NumPad7: case ConsoleKey.D7: spot = 6; break; case ConsoleKey.NumPad8: case ConsoleKey.D8: spot = 7; break; case ConsoleKey.NumPad9: case ConsoleKey.D9: spot = 8; break; default: Console.WriteLine("\nPlease use your numpad or the keys 1-9 to choose a spot to mark."); break; } if (spot != -1) { Console.Clear(); validKeyPressed = MakeMove(marker, spot); } } } // Check to see if the board has no unmarked spots left static bool IsBoardFull() { for (int i = 0; i < board.Length; i++) { if (board[i] == ' ') { return false; } } return true; } // Check a set of numbers static bool CheckWinSet(char marker, int a, int b, int c) { return ((board[a] == marker) && (board[b] == marker) && (board[c] == marker)); } // Check to see if someone has won or if there is a tie // Return a string to annouce the game's final state static string CheckWinner(char marker) { while(true) { // Horizontal Win if (gameOver = CheckWinSet(marker, 0, 1, 2)) { break; } if (gameOver = CheckWinSet(marker, 3, 4, 5)) { break; } if (gameOver = CheckWinSet(marker, 6, 7, 8)) { break; } // Vertical Win if (gameOver = CheckWinSet(marker, 0, 3, 6)) { break; } if (gameOver = CheckWinSet(marker, 1, 4, 7)) { break; } if (gameOver = CheckWinSet(marker, 2, 5, 8)) { break; } // Diagonal Win if (gameOver = CheckWinSet(marker, 0, 4, 8)) { break; } if (gameOver = CheckWinSet(marker, 2, 4, 6)) { break; } break; } if (gameOver) { return marker + " wins!\n"; } // Tie Game if (IsBoardFull()) { gameOver = true; return "No one wins, it's a tie!\n"; } return null; } // Introduce the game to users and explain how to play static void Intro() { Console.WriteLine("\n\n Welcome to Tic Tac Toe!\n\n"); Console.WriteLine(" Currently you can only play against another human player."); Console.WriteLine(" To pick a spot on the Tic Tac Toe board use the keypad or the numbers 1-9."); Console.WriteLine(" You must have num-lock on to be able to use the keypad for input."); Console.WriteLine(" The keypad positions correspond to the Tic Tac Toe board."); // Draws a tic tac toe board with numbers in the spaces to correspond with the numpad Console.WriteLine("\n\n 7 | 8 | 9 "); Console.WriteLine(" ---+---+---"); Console.WriteLine(" 4 | 5 | 6 "); Console.WriteLine(" ---+---+---"); Console.WriteLine(" 1 | 2 | 3\n\n"); Console.WriteLine(" Press any key to start the game!"); Console.ReadKey(true); Console.Clear(); } // The Tic Tac Toe Game's heart static void TicTacToeGame() { char playerTurn = 'O'; string winner = ""; Intro(); DrawBoard(); while (!gameOver) { Console.WriteLine("It is " + playerTurn + "'s turn to move!"); TakeInput(playerTurn); DrawBoard(); winner = CheckWinner(playerTurn); playerTurn = (playerTurn == 'O') ? 'X' : 'O'; } Console.WriteLine(winner); } static void PlayAgain() { Console.WriteLine("Would you like to play again? [Y]es/[N]o?\n"); string response = Console.ReadLine(); switch (response.ToLower()) { case "yes": case "y": gameOver = false; Console.Clear(); ClearBoard(); break; case "no": case "n": newGame = false; Console.Clear(); ClearBoard(); break; default: break; } } static void Main() { // While loop that runs through the intro and a full game of human vs. human tic tac toe // The loop continues until the user responds with a n or no to another game of tic tac toe while (newGame) { TicTacToeGame(); PlayAgain(); } } }}[/source]

PARTNERS