Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualKhatharr

Posted 19 November 2012 - 06:56 PM

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. Posted Image

Also, I realized last night that I posted something wrong earlier:
[source lang="csharp"]//this is not rightstatic bool CheckWinSet(char board[], int a, int b, int c) { return (board[a] == board[b]) && (board[b] == 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

#4Khatharr

Posted 19 November 2012 - 06:48 PM

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. Posted Image

Also, I realized last night that I posted something wrong earlier:
[source lang="csharp"]//this is not rightstatic bool CheckWinSet(char board[], int a, int b, int c) { return (board[a] == board[b]) && (board[b] == 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, etc

#3Khatharr

Posted 19 November 2012 - 06:42 PM

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. Posted Image

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

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

#2Khatharr

Posted 19 November 2012 - 06:41 PM

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. Posted Image

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

Should be:

[source lang="csharp"]static bool CheckWinSet(char board[], int a, int b, int c, char mark) { return (mark == board[a]) && (board[a] == board[b]) && (board[b] == board[c]);}[/source]

#1Khatharr

Posted 19 November 2012 - 06:39 PM

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. :)

PARTNERS