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
-----

Topics I've Started

Completed Tic Tac Toe - Critical Critiques and Advice

15 November 2012 - 05:32 PM

Hello, my name is Maurice Harris and recently while learning my first language(C#) I have completed my first game, Tic Tac Toe. I am here before you all asking for critique and advice on my design of the game Tic Tac Toe. Although I am relieved to have finally completed my first game I feel as though it could use serious improvement design-wise and would like helpful advice on how to improve not only my game but also my programming skills so I can become a much better programmer and avoid the headaches I faced making what I thought would be a simple game to create. So without further ado I present my version of the classic Tic Tac Toe.

[source lang="csharp"]/** Tic Tac Toe - by Maurice Harris* Last Modified: 11/15/12*/using System;namespace TicTacToeGame{ class TicTacToe { static bool gameOver = false; // Draws a tic tac toe board static void DrawBoard(char[] board) { 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(char[] board) { // Loop through char array elements and set them equal to ' ' for (int i = 0; i < board.Length; i++) { board[i] = ' '; } } // Draws a tic tac toe board with numbers in the spaces to correspond with the numpad static void DrawExampleBoard() { Console.WriteLine("\n\n 7 | 8 | 9 "); Console.WriteLine(" ---+---+---"); Console.WriteLine(" 4 | 5 | 6 "); Console.WriteLine(" ---+---+---"); Console.WriteLine(" 1 | 2 | 3\n\n"); } // Checks the board to see if the space is taken up by an O or X static bool SpaceFree(char[] board, 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[] board, char marker, int move) { if (SpaceFree(board, 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[] board, 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; // Do-While loop to catch valid input from the user do { 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: if (MakeMove(board, marker, 0)) { validKeyPressed = true; break; } break; case ConsoleKey.NumPad2: case ConsoleKey.D2: if (MakeMove(board, marker, 1)) { validKeyPressed = true; break; } break; case ConsoleKey.NumPad3: case ConsoleKey.D3: if (MakeMove(board, marker, 2)) { validKeyPressed = true; break; } break; case ConsoleKey.NumPad4: case ConsoleKey.D4: if (MakeMove(board, marker, 3)) { validKeyPressed = true; break; } break; case ConsoleKey.NumPad5: case ConsoleKey.D5: if (MakeMove(board, marker, 4)) { validKeyPressed = true; break; } break; case ConsoleKey.NumPad6: case ConsoleKey.D6: if (MakeMove(board, marker, 5)) { validKeyPressed = true; break; } break; case ConsoleKey.NumPad7: case ConsoleKey.D7: if (MakeMove(board, marker, 6)) { validKeyPressed = true; break; } break; case ConsoleKey.NumPad8: case ConsoleKey.D8: if (MakeMove(board, marker, 7)) { validKeyPressed = true; break; } break; case ConsoleKey.NumPad9: case ConsoleKey.D9: if (MakeMove(board, marker, 8)) { validKeyPressed = true; break; } break; default: Console.WriteLine("\nPlease use your numpad or the keys 1-9 to choose a spot to mark."); break; } } while (!validKeyPressed); } // Check to see if the board has no unmarked spots left static bool BoardFull(char[] board) { for (int i = 0; i < board.Length; i++) { if (board[i] == ' ') { return false; } } return true; } // 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[] board, char marker) { // Horizontal Win if ((board[0] == marker) && (board[1] == marker) && (board[2] == marker)) { gameOver = true; return marker + " wins!\n"; } else if ((board[3] == marker) && (board[4] == marker) && (board[5] == marker)) { gameOver = true; return marker + " wins!\n"; } else if ((board[6] == marker) && (board[7] == marker) && (board[8] == marker)) { gameOver = true; return marker + " wins!\n"; } // Vertical Win else if ((board[0] == marker) && (board[3] == marker) && (board[6] == marker)) { gameOver = true; return marker + " wins!\n"; } else if ((board[1] == marker) && (board[4] == marker) && (board[7] == marker)) { gameOver = true; return marker + " wins!\n"; } else if ((board[2] == marker) && (board[5] == marker) && (board[8] == marker)) { gameOver = true; return marker + " wins!\n"; } // Diagonal Win else if ((board[0] == marker) && (board[4] == marker) && (board[8] == marker)) { gameOver = true; return marker + " wins!\n"; } else if ((board[2] == marker) && (board[4] == marker) && (board[6] == marker)) { gameOver = true; return marker + " wins!\n"; } // Tie Game else if (BoardFull(board)) { 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."); DrawExampleBoard(); 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[] board) { int playerTurn = 1; string winner = ""; while (!gameOver) { // Player 1/O's if (playerTurn == 1) { Console.WriteLine("It is O's turn to move!"); TakeInput(board, 'O'); DrawBoard(board); winner = CheckWinner(board, 'O'); playerTurn = 2; } // Player 2/X's turn else { Console.WriteLine("It is X's turn to move!"); TakeInput(board, 'X'); DrawBoard(board); winner = CheckWinner(board, 'X'); playerTurn = 1; } } Console.WriteLine(winner); } static void Main() { // The Tic Tac Toe board char[] board = {' ',' ',' ',' ',' ',' ',' ',' ',' '}; // Used to keep track of whether a new game has started bool newGame = true; // Do-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 do { Intro(); DrawBoard(board); TicTacToeGame(board); 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(board); break; case "no": case "n": newGame = false; Console.Clear(); ClearBoard(board); break; default: break; } } while (newGame == true); } }}[/source]

PARTNERS