Completed Tic Tac Toe - Critical Critiques and Advice

Started by
10 comments, last by Khatharr 11 years, 5 months ago
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]
Advertisement

Small tip:


keyPressed = Console.ReadKey(true);

switch (keyPressed.Key)
{
case ConsoleKey.NumPad1:
case ConsoleKey.D1:
// etc...
}


Can be shortened if you test ConsoleKeyInfo.KeyChar instead:


keyPressed = Console.ReadKey(true);

switch (keyPressed.KeyChar)
{
case '1':
// etc...
}



Nice. That's what I was hoping someone would mention. (I don't use C#, but I knew it was there somewhere.)

Once you have the char you can just convert it to an int and have the value mathematically. (If it's ASCII or Unicode then just sub 0x30 from it and cast it to int.) After that just check the range and you have your number with far less code.

@Trienco - I know it's not something that's derived from OOP, I just figured that OOP seems to be the next step for him and it will move him into that paradigm. smile.png

Also, I realized last night that I posted something wrong earlier:
[source lang="csharp"]
//this is not right
static bool CheckWinSet(char board[], int a, int b, int c) {
return (board[a] == board) && (board == board[c]);
}[/source]

But looking above it seems that you caught and corrected it.

Looking at your most recent post, it looks like you've almost already got a 'Board' or 'Game' class trying to climb out of that. Why not try to make it one?

The class could:

  • Accept and process a request to place a player's mark in a specific location
  • Check for win/tie
  • Draw the board
  • Eventually be configurable for different board sizes, more than 2 players?, etc
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement