Skip to content

Commit

Permalink
Fix illegal move checking on a 3x3 board
Browse files Browse the repository at this point in the history
  • Loading branch information
xriiitox committed Jan 30, 2022
1 parent aa78407 commit 11fc64c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 15 deletions.
83 changes: 73 additions & 10 deletions Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ public class Game {
public int SIZE;
public char[,] board;
public int userX, userY, AIX, AIY;
public string userMoveThree;
public string? userMoveThree;


public Game(int SIZE) {
Expand Down Expand Up @@ -103,31 +103,94 @@ public void UserMove(char player) {
this.userMoveThree = Console.ReadLine();
switch (this.userMoveThree) {
case "topleft":
this.Move(player, 0, 0);
this.SetUserMove(0, 0);
if (this.CheckIllegalMovesPlayer(this.userX, this.userY)) {
Console.WriteLine("\nIllegal move! Please try again. \n");
this.UserMove(player);
}
else {
this.Move(player, this.userX, this.userY);
}
break;
case "topmiddle":
this.Move(player, 1, 0);
this.SetUserMove(1, 0);
if (this.CheckIllegalMovesPlayer(this.userX, this.userY)) {
Console.WriteLine("\nIllegal move! Please try again. \n");
this.UserMove(player);
}
else {
this.Move(player, this.userX, this.userY);
}
break;
case "topright":
this.Move(player, 2, 0);
this.SetUserMove(2, 0);
if (this.CheckIllegalMovesPlayer(this.userX, this.userY)) {
Console.WriteLine("\nIllegal move! Please try again. \n");
this.UserMove(player);
}
else {
this.Move(player, this.userX, this.userY);
}
break;
case "middleleft":
this.Move(player, 0, 1);
this.SetUserMove(0, 1);
if (this.CheckIllegalMovesPlayer(this.userX, this.userY)) {
Console.WriteLine("\nIllegal move! Please try again. \n");
this.UserMove(player);
}
else {
this.Move(player, this.userX, this.userY);
}
break;
case "middle":
this.Move(player, 1, 1);
this.SetUserMove(1, 1);
if (this.CheckIllegalMovesPlayer(this.userX, this.userY)) {
Console.WriteLine("\nIllegal move! Please try again. \n");
this.UserMove(player);
}
else {
this.Move(player, this.userX, this.userY);
}
break;
case "middleright":
this.Move(player, 2, 1);
this.SetUserMove(2, 1);
if (this.CheckIllegalMovesPlayer(this.userX, this.userY)) {
Console.WriteLine("\nIllegal move! Please try again. \n");
this.UserMove(player);
}
else {
this.Move(player, this.userX, this.userY);
}
break;
case "bottomleft":
this.Move(player, 0, 2);
this.SetUserMove(0, 2);
if (this.CheckIllegalMovesPlayer(this.userX, this.userY)) {
Console.WriteLine("\nIllegal move! Please try again. \n");
this.UserMove(player);
}
else {
this.Move(player, this.userX, this.userY);
}
break;
case "bottommiddle":
this.Move(player, 1, 2);
this.SetUserMove(1, 2);
if (this.CheckIllegalMovesPlayer(this.userX, this.userY)) {
Console.WriteLine("\nIllegal move! Please try again. \n");
this.UserMove(player);
}
else {
this.Move(player, this.userX, this.userY);
}
break;
case "bottomright":
this.Move(player, 2, 2);
this.SetUserMove(2, 2);
if (this.CheckIllegalMovesPlayer(this.userX, this.userY)) {
Console.WriteLine("\nIllegal move! Please try again. \n");
this.UserMove(player);
}
else {
this.Move(player, this.userX, this.userY);
}
break;
case null:
Console.WriteLine("\nIllegal move! Please try again. \n");
Expand Down
15 changes: 10 additions & 5 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ public static void Main() {
} while (!gameEnd);
Thread.Sleep(1000);
Console.Write("\nWould you like to play again? (y/n): ");
string playAgain = Console.ReadLine();
if (playAgain == "y") {
Main();
} else {
return;
string? playAgain = Console.ReadLine();
switch (playAgain) {
case "y":
Main();
break;
case "n":
return;
default:
Console.WriteLine("Invalid input. Exiting...");
return;
}
}
}
Expand Down

0 comments on commit 11fc64c

Please sign in to comment.