I'm attempting to convert the Board and CheWin sections into classes. Is there a way to do just that?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace TicTacToe
{
class TicTacToe
{
static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9' };
static int player = 1;
static int choice;
static int flag = 0;
static void Main(string[] args)
{
do
{
Console.Clear();
Console.WriteLine("Player 1 : 'X' \nPlayer 2 : 'O'\n");
if (player % 2 == 0)
{
Console.WriteLine("Player 2 Turn");
}
else
{
Console.WriteLine("Player 1 Turn");
}
Console.WriteLine("\n");
Board();
choice = int.Parse(Console.ReadLine());
if (arr[choice] != 'X' && arr[choice] != 'O')
{
if (player % 2 == 0)
{
arr[choice] = 'O';
player++;
}
else
{
arr[choice] = 'X';
player++;
}
}
else
{
Console.WriteLine("Sorry the row {0} is already marked with {1}",
choice, arr[choice], "\n");
Console.WriteLine("Please wait a second board is loading
again.......");
Thread.Sleep(1000);
}
flag = CheckWin();
} while (flag != 1 && flag != -1);
Console.Clear();
Board();
if (flag == 1)
{
Console.WriteLine("Player {0} has won", (player % 2) + 1);
}
else
{
Console.WriteLine("Game has ended in a draw!!!");
}
Console.ReadLine();
}
private static void Board()
{
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", arr[7], arr[8],
arr[9]);
Console.WriteLine("___|___|___");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", arr[4], arr[5],
arr[6]);
Console.WriteLine("___|___|____");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", arr[1], arr[2],
arr[3]);
Console.WriteLine(" | | ");
}
private static int CheckWin()
{
if (arr[1] == arr[2] && arr[2] == arr[3])
{
return 1;
}
else if (arr[4] == arr[5] && arr[5] == arr[6])
{
return 1;
}
else if (arr[7] == arr[8] && arr[8] == arr[9])
{
return 1;
}
else if (arr[1] == arr[4] && arr[4] == arr[7])
{
return 1;
}
else if (arr[2] == arr[5] && arr[5] == arr[8])
{
return 1;
}
else if (arr[3] == arr[6] && arr[6] == arr[9])
{
return 1;
}
else if (arr[1] == arr[5] && arr[5] == arr[9])
{
return 1;
}
else if (arr[3] == arr[5] && arr[5] == arr[7])
{
return 1;
}
else if (arr[1] != '1' && arr[2] != '2' &&
arr[3] != '3' && arr[4] != '4' && arr[5] != '5'
&& arr[6] != '6' && arr[7] != '7' && arr[8]
!= '8' && arr[9] != '9')
{
return -1;
}
else
{
return 0;
}
}
}
}
I have modified above code so that all the mentioned functions are changed from static to class Functions. Also changed static variables to class variables.
Note: class TicTacToe is having all the variables and functions. In main(), one object is created of TicTacToe class and play() in TicTacToe is called for playing the game. All references to class variables will be done through "this" keyword to specify the object variables.
KINDLY GIVE THUMBS UP
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace TicTacToe
{
class TicTacToe
{
private char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9' };
private int player = 1;
private int choice;
private int flag = 0;
public int CheckWin()
{
if (this.arr[1] == this.arr[2] &&
this.arr[2] == this.arr[3])
{
return 1;
}
else if (this.arr[4] == this.arr[5] &&
this.arr[5] == this.arr[6])
{
return 1;
}
else if (this.arr[7] == this.arr[8] &&
this.arr[8] == this.arr[9])
{
return 1;
}
else if (this.arr[1] == this.arr[4]
&& this.arr[4] == this.arr[7])
{
return 1;
}
else if (this.arr[2] == this.arr[5] &&
this.arr[5] == this.arr[8])
{
return 1;
}
else if (this.arr[3] == this.arr[6] &&
this.arr[6] == this.arr[9])
{
return 1;
}
else if (this.arr[1] == this.arr[5]
&& this.arr[5] == this.arr[9])
{
return 1;
}
else if (this.arr[3] == this.arr[5] &&
this.arr[5] == this.arr[7])
{
return 1;
}
else if (this.arr[1] != '1' &&
this.arr[2] != '2' && this.arr[3] != '3' &&
this.arr[4] != '4' && this.arr[5] != '5' &&
this.arr[6] != '6' && this.arr[7] != '7' &&
this.arr[8] != '8' && this.arr[9] != '9')
{
return -1;
}
else
{
return 0;
}
}
public void Board()
{
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}",
this.arr[7], this.arr[8], this.arr[9]);
Console.WriteLine("___|___|___");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}",
this.arr[4], this.arr[5], this.arr[6]);
Console.WriteLine("___|___|____");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}",
this.arr[1], this.arr[2], this.arr[3]);
Console.WriteLine(" | | ");
}
public void play(){
do
{
Console.Clear();
Console.WriteLine("Player 1 : 'X' \nPlayer 2 : 'O'\n");
if (this.player % 2
== 0)
{
Console.WriteLine("Player 2 Turn");
}
else
{
Console.WriteLine("Player 1 Turn");
}
Console.WriteLine("\n");
this.Board();
this.choice =
int.Parse(Console.ReadLine());
if
(this.arr[this.choice] != 'X' && this.arr[this.choice] !=
'O')
{
if (this.player % 2 ==
0)
{
this.arr[this.choice] =
'O';
this.player++;
}
else
{
this.arr[this.choice] =
'X';
this.player++;
}
}
else
{
Console.WriteLine("Sorry
the row {0} is already marked with {1}", this.choice,
this.arr[this.choice], "\n");
Console.WriteLine("Please wait a second board is loading
again.......");
Thread.Sleep(1000);
}
this.flag =
this.CheckWin();
} while (this.flag != 1 && this.flag !=
-1);
Console.Clear();
this.Board();
if (this.flag == 1)
{
Console.WriteLine("Player {0} has won",
(this.player % 2) + 1);
}
else
{
Console.WriteLine("Game has ended in a
draw!!!");
}
Console.ReadLine();
}
static void Main(string[] args)
{
TicTacToe game = new TicTacToe();
game.play();
}
}
}
I'm attempting to convert the Board and CheWin sections into classes. Is there a way to...
The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() { char board[SIZE]; int player, choice, win, count; char mark; count = 0; // number of boxes marked till now initBoard(board); // start the game player = 1; // default player mark = 'X'; // default mark do { displayBoard(board); cout << "Player " << player << "(" << mark...
Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending). Please ensure the resulting code completes EACH part of the following prompt: "Your task is to create a game of Tic-Tac-Toe using a 2-Dimensional String array as the game board. Start by creating a Board class that holds the array. The constructor should use a traditional for-loop to fill the array with "blank" Strings (eg. "-"). You may want to include other instance data......
Greetings everyone I am having a little trouble with this one. I bolded my switch case which should work but its currently not. If a user selected course option 1 twice it just says you enrolled in course 1 and course 1. It should stop it by executing case -2. Need a new set of eyes. - Thanks using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleRegisterStudent { class Program { static void Main(string[] args) { (new...
C#, I am getting error placing them all in one file pls set them in the order that all 3 work in one file. Thanks //Program 1 using System; class MatrixLibrary { public static int[,] Matrix_Multi(int[,] a, int [,]b){ int r1= a.GetLength(0); int c1 = a.GetLength(1); int c2 = b.GetLength(1); int r2 = b.GetLength(0); if(r2 != c2){ Console.WriteLine("Matrices cannot be multiplied together.\n"); return null; }else { int[,] c = new int[r1, c2]; for (int i = 0; i <...
There is a problem with thecode. I get the error messages " 'board' was not declared in this scope", \src\TicTacToe.cpp|7|error: expected unqualified-id before ')' token| \src\TicTacToe.cpp|100|error: expected declaration before '}' token| Here is the code #ifndef TICTACTOE_H #define TICTACTOE_H #include class TicTacToe { private: char board [3][3]; public: TicTacToe () {} void printBoard(); void computerTurn(char ch); bool playerTurn (char ch); char winVerify(); }; ************************************ TicTacToe.cpp #endif // TICTACTOE_H #include "TicTacToe.h" #include #include using namespace std; TicTacToe() { for(int i =...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
For this exercise, you will complete the TicTacToe Board that we started in the 2D Arrays Lesson. We will add a couple of methods to the TicTacToe class. To track whose turn it is, we will use a counter turn. This is already declared as a private instance variable. Create a getTurn method that returns the value of turn. Other methods to implement: printBoard()- This method should print the TicTacToe array onto the console. The board should include numbers that...
I need help with this method (public static int computer_move(int board[][])) . When I run it and play the compute.The computer enters the 'O' in a location that the user has all ready enter it sometimes. I need to fix it where the computer enters the 'O' in a location the user has not enter the "X' already package tictactoe; import java.util.Scanner; public class TicTacToeGame { static final int EMPTY = 0; static final int NONE = 0; static final...
Hi, I am a student in college who is trying to make a tic tac toe game on Java. Basically, we have to make our own game, even ones that already exist, on Java. We also have to implement an interface, that I have no idea exactly. The professor also stated we need at least 2 different data structures and algorithms for the code. The professor said we could use code online, we'd just have to make some of our...
C# coding error with Cengage: MoveEstimator.cs(15,58): error CS1525: Unexpected symbol `total' Compilation failed: 1 error(s), 0 warnings Cannot open assembly 'MoveEstimator.exe': No such file or directory. Here is my code and the prompt as well Malcolm Movers charges a base rate of $200 per move plus $150 per hour and $2 per mile. Write a program named MoveEstimator that prompts a user for and accepts estimates for the number of hours for a job and the number of miles involved...