0 Replies - 197 Views - Last Post: 30 April 2014 - 04:09 PM Rate Topic: -----

#1 jafern32  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-April 14

Tic-Tac-Toe C# If statement problem

Posted 30 April 2014 - 04:09 PM

Hello everyone, I am new to this website and i was hoping someone can shed some light for me on this program. I have tried countless ways to fix the issue i am having with this program. For the most part, the program works. The problem i am having is when "player 1" chooses "X" and if "player 2" tries to use the same box to put "O", it does not give the output error "Console.WriteLine("Warning: This box has already been used.");". Also, after completeing a game on whoever wins and you want to play again, the board does not reset. Your help is greatly appreciated.

[code] using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string A = "1";
string B = "2";
string C = "3";
string D = "4";
string E = "5";
string F = "6";
string G = "7";
string H = "8";
string I = "9";
string turn = "player1";
string won = "false";
string playAgain = "false";
string player1, player2, selection;

Console.WriteLine("Welcome to Tic-Tac-Toe");
Console.WriteLine("");
Console.WriteLine("Press any key to continue.");
Console.ReadLine();
Console.Clear();
Console.WriteLine("Please enter the name of the first player.");
player1 = Console.ReadLine();
Console.WriteLine("Please enter the name of the second player.");
player2 = Console.ReadLine();



Start: // goto statement reference http://msdn.microsof...y/b34dt9cd.aspx
Console.Clear();


if (turn == "player1")
{
turn = "player2";
Console.Clear();
Console.WriteLine("Player: " + player1 + "'s turn!");
Console.WriteLine("");


//Drawing the board
//1|2|3
//4|5|6
//7|8|9
Console.WriteLine(A + "|" + B + "|" + C);
Console.WriteLine(D + "|" + E + "|" + F);
Console.WriteLine(G + "|" + H + "|" + I);
Console.WriteLine("******************");
Console.WriteLine("Please make a selection");
selection = Console.ReadLine();

if (selection == "1")
{
if (A == "1")
{
A = "X";
}
else A = "O"; A = "X";
{
Console.WriteLine("Warning: This box has already been used.");
}
}

if (selection == "2")
{
if (B == "2")
{
B = "X";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "3")
{
if (C == "3")
{
C = "X";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "4")
{
if (D == "4")
{
D = "X";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "5")
{
if (E == "5")
{
E = "X";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "6")
{
if (F == "6")
{
F = "X";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "7")
{
if (G == "7")
{
G = "X";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "8")
{
if (H == "8")
{
H = "X";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "9")
{
if (I == "9")
{
I = "X";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}

if (A == B & B == C) // X|X|X
{
won = "true";
Console.WriteLine("Congratulations! " + player1 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (D == E & E == F) // X|X|X
{
won = "true";
Console.WriteLine("Congratulations! " + player1 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (G == H & H == I) // X|X|X
{
won = "true";
Console.WriteLine("Congratulations! " + player1 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";

}
else
{
playAgain = "false";
}
}
if (A == D & D == G) // "X" STRAIGHT UP & DOWN
{
won = "true";
Console.WriteLine("Congratulations! " + player1 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (B == E & E == H) // "X" STRAIGHT UP & DOWN
{
won = "true";
Console.WriteLine("Congratulations! " + player1 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (C == F & F == I) // "X" STRAIGHT UP & DOWN
{
won = "true";
Console.WriteLine("Congratulations! " + player1 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (G == E & E == C) // "X" DIAGONAL
{
won = "true";
Console.WriteLine("Congratulations! " + player1 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (A == E & E == I) // "X" DIAGONAL
{
won = "true";
Console.WriteLine("Congratulations! " + player1 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";

}
else
{
playAgain = "false";
}
}



}

if (turn == "player2")
{
turn = "player1";
Console.Clear();
Console.WriteLine("Player: " + player2 + "'s turn!");
Console.WriteLine("");
Console.WriteLine(A + "|" + B + "|" + C);
Console.WriteLine(D + "|" + E + "|" + F);
Console.WriteLine(G + "|" + H + "|" + I);
Console.WriteLine("******************");
Console.WriteLine("Please make a selection");
selection = Console.ReadLine();

if (selection == "1")
{
if (A == "1")
{
A = "O";
}
else
{
selection = "1";
Console.WriteLine("Warning: This box has already been used.");
}
}

if (selection == "2")
{
if (B == "2")
{
B = "O";
}
else
{

Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "3")
{
if (C == "3")
{
C = "O";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "4")
{
if (D == "4")
{
D = "O";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "5")
{
if (E == "5")
{
E = "O";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "6")
{
if (F == "6")
{
F = "O";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "7")
{
if (G == "7")
{
G = "O";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "8")
{
if (H == "8")
{
H = "O";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}
if (selection == "9")
{
if (I == "9")
{
I = "O";
}
else
{
Console.WriteLine("Warning: This box has already been used.");
}
}

if (A == B & B == C) // O|O|O
{
won = "true";
Console.WriteLine("Congratulations! " + player2 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (D == E & E == F) // O|O|O
{
won = "true";
Console.WriteLine("Congratulations! " + player2 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (G == H & H == I) // O|O|O
{
won = "true";
Console.WriteLine("Congratulations! " + player2 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (A == D & D == G) // "O" STRAIGHT UP & DOWN
{
won = "true";
Console.WriteLine("Congratulations! " + player2 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (B == E & E == H) // "O" STRAIGHT UP & DOWN
{
won = "true";
Console.WriteLine("Congratulations! " + player2 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (C == F & F == I) // "O" STRAIGHT UP & DOWN
{
won = "true";
Console.WriteLine("Congratulations! " + player2 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (G == E & E == C) // "O" DIAGONAL
{
won = "true";
Console.WriteLine("Congratulations! " + player2 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";
}
else
{
playAgain = "false";
}
}
if (A == E & E == I) // "O" DIAGONAL
{
won = "true";
Console.WriteLine("Congratulations! " + player2 + " won!");
Console.WriteLine("");
Console.WriteLine("Do you want to play again? Y / N");
selection = Console.ReadLine();
if (selection == "Y")
{
playAgain = "true";


}
else
{
playAgain = "false";
}

}


}
if (won == "false")
{

goto Start; // reference http://msdn.microsof...y/b34dt9cd.aspx
A = "1";
B = "2";
C = "3";
D = "4";
E = "5";
F = "6";
G = "7";
H = "8";
I = "9";
}
if (playAgain == "true")
{

A = "1";
B = "2";
C = "3";
D = "4";
E = "5";
F = "6";
G = "7";
H = "8";
I = "9";
goto Start; // reference http://msdn.microsof...y/b34dt9cd.aspx
}
else
{
Console.WriteLine("Thank you for playing!");
}
}

}
}
[code]

Is This A Good Question/Topic? 0
  • +

Page 1 of 1