Question: I am trying to write a program that creates a wild tic tac toe game from an extensible framework. I made a base of the

I am trying to write a program that creates a wild tic tac toe game from an extensible framework. I made a base of the program with some glitches for what I can not get the expected output.
I need to
i. cater the game for another mode-human vs human as my current code is only about human vs computer mode.
ii. Games can be played from start to finish, and should be able to be saved and restored from any state of play (i.e. save file). A game should be replayable from any position after being loaded from a save file.
iii. During a game, all moves made by both players should be undoable and redoable (i.e. the full history of moves are tracked). But the history of the moves does not have to be saved into a save file. That is, immediately after loading the saved state from a file, undo and redo operations are not available until new moves have been made.
iv. I tried to include a primitive help system which id accessible within the running game, rather than a separate offline help document to assist users with the available commands. I do not know to actually use it.
Please suggest how to apply the above 4 specific criteria in my following code including some help to erase my bug which is- sometimes the console takes the random number very well, other time it becomes an indefinite loop. Please suggest.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace TwoPlayerBoardGames
{
public class TicTacToe : IBoardGame
{
private int[] board = new int[9];
private Random rand = new Random();
public void Initialize()
{
for (int i =0; i <9; i++)
{
board[i]=0;
}
}
public void DisplayBoard()
{
for (int i =0; i <9; i++)
{
if (board[i]==0)
{
Console.Write(".");
}
else if (board[i]==1)
{
Console.Write("X");
}
else if (board[i]==2)
{
Console.Write("O");
}
if (i ==2|| i ==5|| i ==8)
{
Console.WriteLine();
}
}
}
public void Play()
{
int userTurn =-1;
int computerTurn =-1;
// Human player's turn
while (userTurn ==-1|| board[userTurn]!=0)
{
Console.WriteLine("Please enter a number from 0 to 8");
userTurn = int.Parse(Console.ReadLine());
Console.WriteLine("You typed: "+ userTurn);
}
board[userTurn]=1;
// Computer player's turn
while (computerTurn ==-1|| board[computerTurn]!=0)
{
computerTurn = rand.Next(8);
Console.WriteLine("Computer chooses: "+ computerTurn);
}
board[computerTurn]=2;
}
public bool IsGameOver()
{
// Implement game over logic
// Return true if the game is over, false otherwise
// Example: check for a winner or if the board is full
return false;
}
public void SaveGame(string filename)
{
using (FileStream fileStream = new FileStream(filename, FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, board);
}
Console.WriteLine("Game saved successfully");
}
public void LoadGame(string filename)
{
try
{
using (FileStream fileStream = new FileStream(filename, FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
int[] loadedBoard =(int[])formatter.Deserialize(fileStream);
Array.Copy(loadedBoard, board, 9);
}
Console.WriteLine("Game loaded successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Error loading game: {ex.Message}");
}
}
public void DisplayHelp()
{
Console.WriteLine("Available commands:");
Console.WriteLine("undo: Undo the last move");
Console.WriteLine("save[file_name]: Save the current game state to a file");
Console.WriteLine("load[file_name]: Load a saved game state from a file");
Console.WriteLine("help: Display this help message");
Console.WriteLine("exit: Quit the game.");
}
public void DisplayResult()
{
// Implement displaying result
}
public void SwitchPlayer()
{
// Implement switching player logic if needed

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!