C# programming: Complete the Tic-Tac-Toe game: Download the game template TicTac
ID: 3595146 • Letter: C
Question
C# programming:
Complete the Tic-Tac-Toe game:
Download the game template TicTacToe.zip.
Add Exception throwing to the .Move method as described in the TBD comment
.
Add Exception handling to the Main() method as described in the TBD comment.
Complete the Ended method. The game is over when the grid is full or one player has 3 in a row. Humans are very good at recognizing patterns and can see 3 in a row quite easily. Think about how you would write down a step by step process of detecting this and then implement it. You may discuss your ideas in the forums but do not share code.
Code so far:
Game.cs:
using System;
using System.Security.Cryptography.X509Certificates;
namespace TicTacToe
{
internal class Game
{
char[,] grid = new char[3,3];
public Game()
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
grid[x, y] = '.';
}
}
}
public bool Ended()
{
//TBD: Determine the 'end' condition
return false;
}
internal void Move(int position, char player)
{
//TBD: This function needs validation
//1. Position needs to be in the range 0..9 - if not throw an ArgumentException
//2. The grid cell needs to be unoccupied - if not throw a BadMoveException
// .Net does not have a BadMoveException so you must define a custom exception class
grid[position % 3, (position /3)] = player;
}
public void Print()
{
Console.Clear();
Console.WriteLine("Key");
int cell = 0;
for (int y = 0; y < 3; y++)
{
if (y >= 1) Console.WriteLine("---+---+---");
for (int x = 0; x < 3; x++)
{
if (x >= 1) Console.Write("|");
Console.Write(" {0} ", cell++);
}
Console.WriteLine();
}
Console.WriteLine();
for (int y = 0; y < 3; y++)
{
if (y >= 1) Console.WriteLine("---+---+---");
for (int x = 0; x < 3; x++)
{
if (x >= 1) Console.Write("|");
Console.Write(" {0} ", grid[x,y]);
}
Console.WriteLine();
}
}
}
}
Program.cs:
using System;
namespace TicTacToe
{
class Program
{
static void Main()
{
var game = new Game();
var player = 'X';
while (!game.Ended())
{
game.Print();
//Get an integer position
Console.Write("Enter your move:");
string input;
int position;
do
{
input = Console.ReadLine();
} while (!int.TryParse(input, out position));
//TBD: Handle the Exceptions:
//1. Once you have implemented exceptions in game.Move() you must catch the ones you expect to see here
//and handle them with suitable error messages
//2. Also handle any unexpected exceptions with an "Unknown Error" message. Work out a way to test that code!
game.Move(position, player);
//Switch players
player = (player == 'X') ? 'O' : 'X';
}
}
}
}
Explanation / Answer
The required code is as follows:
//Game.cs:
using System;
using System.Security.Cryptography.X509Certificates;
namespace TicTacToe
{
internal class Game
{
char[,] grid = new char[3,3];
public Game()
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
grid[x, y] = '.';
}
}
}
public bool Ended()
{
//TBD: Determine the 'end' condition
for (int i=0; i<3; i++)
{
if (grid[i,0] == grid[i,1] &&
grid[i,1] == grid[i,2] &&
grid[i,0] != ' ')
return (true);
}
//return(false);
for (int i=0; i<3; i++)
{
if (grid[0,i] == grid[1,i] &&
grid[1,i] == grid[2,i] &&
grid[0,i] != ' ')
return (true);
}
//return(false);
if (grid[0,0] == grid[1,1] &&
grid[1,1] == grid[2,2] &&
grid[0,0] != ' ')
return(true);
if (grid[0,2] == grid[1,1] &&
grid[1,1] == grid[2,0] &&
grid[0,2] != ' ')
return(true);
return false;
}
internal void Move(int position, char player)
{
//TBD: This function needs validation
//1. Position needs to be in the range 0..9 - if not throw an ArgumentException
//2. The grid cell needs to be unoccupied - if not throw a BadMoveException
// .Net does not have a BadMoveException so you must define a custom exception class
if(position<0 || position>9)
{
throw new System.ArgumentException("Parameter should be between 0-9", "position");
}
if(grid[position % 3, (position /3)] != '.')
{
throw new Exception("Bad move exception");
}
grid[position % 3, (position /3)] = player;
}
public void Print()
{
Console.Clear();
Console.WriteLine("Key");
int cell = 0;
for (int y = 0; y < 3; y++)
{
if (y >= 1) Console.WriteLine("---+---+---");
for (int x = 0; x < 3; x++)
{
if (x >= 1) Console.Write("|");
Console.Write(" {0} ", cell++);
}
Console.WriteLine();
}
Console.WriteLine();
for (int y = 0; y < 3; y++)
{
if (y >= 1) Console.WriteLine("---+---+---");
for (int x = 0; x < 3; x++)
{
if (x >= 1) Console.Write("|");
Console.Write(" {0} ", grid[x,y]);
}
Console.WriteLine();
}
}
}
}
//Program.cs:
//using System;
namespace TicTacToe
{
class Program
{
static void Main()
{
var game = new Game();
var player = 'X';
while (!game.Ended())
{
game.Print();
//Get an integer position
Console.Write("Enter your move:");
string input;
int position;
do
{
input = Console.ReadLine();
} while (!int.TryParse(input, out position));
//TBD: Handle the Exceptions:
//1. Once you have implemented exceptions in game.Move() you must catch the ones you expect to see here
//and handle them with suitable error messages
try
{
game.Move(position, player);
}
catch (ArgumentException e)
{
System.Diagnostics.Debug.Write(e.Message);
// This is wrong.
throw e;
// Should be this:
// throw;
}
game.Move(position, player);
//Switch players
player = (player == 'X') ? 'O' : 'X';
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.