Hello, I have been working on an assignment for a programming class where I am to make a Tic-Tac-Toe Game with a C# Console Application for two human players.. I have it set up so that you place an X or O by typing in the number of the space. I've been working on it for a couple days and am now stuck on a part on how to alternate between X's and O's. Right now I have it just so that it only enters an X and was wondering how I would go about alternating between the for each turn. Let me show you what I have so far. I had to create a separate class since I kept getting this error where it would tell me I needed an object reference since the variables couldn't be static because they needed to be able to change, so I'll show that first:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TicTacToe
{
class Class1
{
//create array for tic tac toe game
public string[,] tictactoeArray = new string[3, 3] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } };
public void Display()
{
Console.WriteLine("\n{0} {1} {2}\n", tictactoeArray[0, 0], tictactoeArray[0, 1], tictactoeArray[0, 2]);
Console.WriteLine("{0} {1} {2}\n", tictactoeArray[1, 0], tictactoeArray[1, 1], tictactoeArray[1, 2]);
Console.WriteLine("{0} {1} {2}\n\n", tictactoeArray[2, 0], tictactoeArray[2, 1], tictactoeArray[2, 2]);
}
}
}
And here is the main code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TicTacToe
{
class Program
{
//Note: I created another class for this assignment since I had problems with an error related to the array not being static and
//if I made it static I don't think it can change.
static void Main(string[] args)
{
//introduction
Console.WriteLine("Welcome to Tic Tac Toe, X goes first.");
Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");
//creating object necessary to prevent error since variables nonstatic
Class1 Obj1;
Obj1 = new Class1();
//create tic tac toe board
Obj1.Display();
//be able to receive input
string input = Console.ReadLine();
//loop for constantly checking input
do
{
//when user enters a number, change number to X or O
if (input == "1")
{
Obj1.tictactoeArray[0, 0] = "X";
Obj1.Display();
Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");
}
if (input == "2")
{
Obj1.tictactoeArray[0, 1] = "X";
Obj1.Display();
Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");
}
if (input == "3")
{
Obj1.tictactoeArray[0, 2] = "X";
Obj1.Display();
Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");
}
if (input == "4")
{
Obj1.tictactoeArray[1, 0] = "X";
Obj1.Display();
Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");
}
if (input == "5")
{
Obj1.tictactoeArray[1, 1] = "X";
Obj1.Display();
Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");
}
if (input == "6")
{
Obj1.tictactoeArray[1, 2] = "X";
Obj1.Display();
Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");
}
if (input == "7")
{
Obj1.tictactoeArray[2, 0] = "X";
Obj1.Display();
Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");
}
if (input == "8")
{
Obj1.tictactoeArray[2, 1] = "X";
Obj1.Display();
Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");
}
if (input == "9")
{
Obj1.tictactoeArray[2, 2] = "X";
Obj1.Display();
Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");
}
//If an invalid input is put in, tell user to put in valid input
if (input != "q" && input != "quit" && input != "1" && input != "2" && input != "3" && input != "4" && input != "5" && input != "6" && input != "7" && input != "8" && input != "9")
{
Console.WriteLine("Please enter an unoccupied number, or press q or quit to exit");
Obj1.Display();
}
input = Console.ReadLine();
} while (input != "q" || input != "quit");
//if user enters quit or q, exit application
if (input == "quit" || input == "q")
{
Environment.Exit(0);
}
//prevents application from closing when starts
Console.ReadLine();
}
//Methods area
//create a boolean to tell whether board is completed
bool BoardComplete = false;
}
}
Hopefully someone can give me some guidance on what I should do next.