Solve My Tic Tac Toe Program Homework Problem

  • Thread starter Veronica_Oles
  • Start date
  • Tags
    Program Toe
In summary: This else statement will make sure that if the board position has a 1 in it that the player has won the game, so this will increase the Xwins variable } return result; } }In summary, the player using the letter X can go and make a move, but if the player using the letter O makes a move then the turn increases by 1. The player using the letter X can make a move every other turn, but the player using the letter O can only make a move once every two turns. The player using the letter X can win if the positions on the board 0,0 , 0,1 and 0,2
  • #1
Veronica_Oles
142
3

Homework Statement


Making tic tac toe program!

Homework Equations

The Attempt at a Solution


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TTT
{
public partial class Form1 : Form
{

static int turn = 0;// This variable will be used store the amount of turns the players will play
static int[,] board = new int[3, 3];// This array will be used
static bool winner = false;// This boolean will be used store winner, winner is set to false because no one has won at beginning becomes true if someone wins
static int Owins, Xwins, CatsGame = 0;// This variable will be used to store the number of times O wins, X wins, and ties

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{


button14.Visible = true;
button15.Visible = true;

}

private void button4_Click(object sender, EventArgs e)
{

}

private void button5_Click(object sender, EventArgs e)
{
if (turn % 2 == 0)// If the players turn is mod by 2 and equal to 0 that means that the player using the letter O can go and make a move
{

button5.Text = "O";// This will output letter O
board[0, 0] = 2;// The player using letter O is equal to 2
button5.Enabled = false;// Once player uses that button it becomes enabled so that user cannot go back and change it
turn++;// After player makes a move this will loop around so that the next move can be made
}
else// If the players turn is mod by 2 and equal to a number greater than 0 that means that the player using the letter X can go and make a move
{

button5.Text = "X";// This will output the letter X
board[0, 0] = 1;// The player using letter X is equal to 1
button5.Enabled = false;// Once player uses that button it becomes enabled so that user cannot go back and change it
turn++;// After player makes a move this will loop around so that the next move can be made
}

textBox2.Text = turn.ToString();// This textbox will display the amount of turns the players use in order to win a game
textBox1.Text = check_winner().ToString();// This textbox will be used to display if there's a winner by outputting the word true and if no winner false
if (winner == false && turn == 9)// However, if there is no winner by the ninth turn then the textbox will output the words Cats Game
{
textBox1.Text = "Cats Game";
} }

private void button6_Click(object sender, EventArgs e)
{
if (turn % 2 == 0)
{

button6.Text = "O";
board[0, 1] = 2;
button6.Enabled = false;
turn++;
}
else
{

button6.Text = "X";
board[0, 1] = 1;
button6.Enabled = false;
turn++;
}
textBox2.Text = turn.ToString();
textBox1.Text = check_winner().ToString();
if (winner == false && turn == 9)
{
textBox1.Text = "Cats Game";
} }

private void button7_Click(object sender, EventArgs e)
{
if (turn % 2 == 0)
{

button7.Text = "O";
board[0, 2] = 2;
button7.Enabled = false;
turn++;
}
else
{

button7.Text = "X";
board[0, 2] = 1;
button7.Enabled = false;
turn++;
}
textBox2.Text = turn.ToString();
textBox1.Text = check_winner().ToString();
if (winner == false && turn == 9)
{
textBox1.Text = "Cats Game";
} }

static bool check_winner()// This is a new method that will check if a player has won a game
{

bool result = false;// The boolean result is set to false, when a player wins it becomes true
if (board[0, 0] == 1 && board[0, 1] == 1 && board[0, 2] == 1 || board[0, 0] == 2 && board[0, 1] == 2 && board[0, 2] == 2)
// This if statement checks if there is a winner by checking the rows and columns as well as the player's X or O to determine the actual winner
// For this specific if statement if the positions on the board 0,0 , 0,1 and 0,2 are equal to 1 then x will win if it is equal to 2 then o will win
{ if (board[0, 0] == 1)// If the position of the board has a 1
{

Xwins++;// This means that X will win }

else// If the position of the board has a 2
{

Owins++;// This means that O wins
}
result = true;// If any of these statements proove to be true then the result will be true meaning a player had won }

else if (board[1, 0] == 1 && board[1, 1] == 1 && board[1, 2] == 1 || board[1, 0] == 2 && board[1, 1] == 2 && board[1, 2] == 2)
{
if (board[1, 0] == 1)
{

Xwins++;
}

else
{

Owins++;
}
result = true;
} else if (board[2, 0] == 1 && board[2, 1] == 1 && board[2, 2] == 1 || board[2, 0] == 2 && board[2, 1] == 2 && board[2, 2] == 2)
{
if (board[2, 0] == 1)
{

Xwins++;
}

else
{

Owins++;
}
result = true;
}

else if (board[0, 0] == 1 && board[1, 0] == 1 && board[2, 0] == 1 || board[0, 0] == 2 && board[1, 0] == 2 && board[2, 0] == 2)
{
if (board[0, 0] == 1)
{

Xwins++;
}

else
{

Owins++;
}
result = true;
}

else if (board[0, 1] == 1 && board[1, 1] == 1 && board[2, 1] == 1 || board[0, 1] == 2 && board[1, 1] == 2 && board[2, 1] == 2)
{
if (board[0, 1] == 1)
{

Xwins++;
}

else
{

Owins++;
}
result = true;
}

else if (board[0, 2] == 1 && board[1, 2] == 1 && board[2, 2] == 1 || board[0, 2] == 2 && board[1, 2] == 2 && board[2, 2] == 2)
{
if (board[0, 2] == 1)
{

Xwins++;
}

else
{

Owins++;
}
result = true;
}

else if (board[0, 0] == 1 && board[1, 1] == 1 && board[2, 2] == 1 || board[0, 0] == 2 && board[1, 1] == 2 && board[2, 2] == 2)
{
if (board[0, 0] == 1)
{

Xwins++;
}

else
{

Owins++;
}
result = true;
}

else if (board[0, 2] == 1 && board[1, 1] == 1 && board[2, 0] == 1 || board[0, 2] == 2 && board[1, 1] == 2 && board[2, 0] == 2)
{
if (board[0, 2] == 1)
{

Xwins++;
}

else
{

Owins++;
}
result = true;
}
return result;

}

private void button8_Click(object sender, EventArgs e)
{

if (turn % 2 == 0)
{

button8.Text = "O";
board[1, 0] = 2;
button8.Enabled = false;
turn++;
}
else
{

button8.Text = "X";
board[1, 0] = 1;
button8.Enabled = false;
turn++;
}
textBox2.Text = turn.ToString();
textBox1.Text = check_winner().ToString();

if (winner == false && turn == 9)
{
textBox1.Text = "Cats Game";
} }

private void button9_Click(object sender, EventArgs e)
{
if (turn % 2 == 0)
{

button9.Text = "O";
board[1, 1] = 2;
button9.Enabled = false;
turn++;
}
else
{

button9.Text = "X";
board[1, 1] = 2;
button9.Enabled = false;
turn++;
}
textBox2.Text = turn.ToString();
textBox1.Text = check_winner().ToString();
if (winner == false && turn == 9)
{
textBox1.Text = "Cats Game";
} }

private void button10_Click(object sender, EventArgs e)
{
if (turn % 2 == 0)
{

button10.Text = "O";
board[1, 2] = 2;
button10.Enabled = false;
turn++;
}
else
{

button10.Text = "X";
board[1, 2] = 1;
button10.Enabled = false;
turn++;
}
textBox1.Text = check_winner().ToString();
textBox2.Text = turn.ToString();
if (winner == false && turn == 9)
{
textBox1.Text = "Cats Game";
} }

private void button11_Click(object sender, EventArgs e)
{
if (turn % 2 == 0)
{

button11.Text = "O";
board[2, 0] = 2;
button11.Enabled = false;
turn++;
}
else
{

button11.Text = "X";
board[2, 0] = 1;
button11.Enabled = false;
turn++;
}
textBox2.Text = turn.ToString();
textBox1.Text = check_winner().ToString();
if (winner == false && turn == 9)
{
textBox1.Text = "Cats Game";
} }

private void button12_Click(object sender, EventArgs e)
{
if (turn % 2 == 0)
{

button12.Text = "O";
board[2, 1] = 2;
button12.Enabled = false;
turn++;
}
else
{

button12.Text = "X";
board[2, 1] = 1;
button12.Enabled = false;
turn++;
}
textBox2.Text = turn.ToString();
textBox1.Text = check_winner().ToString();
if (winner == false && turn == 9)
{
textBox1.Text = "Cats Game";
} }

private void button13_Click(object sender, EventArgs e)
{
if (turn % 2 == 0)
{

button13.Text = "O";
board[2, 2] = 2;
button13.Enabled = false;
turn++;
}
else
{

button13.Text = "X";
board[2, 2] = 1;
button13.Enabled = false;
turn++;
}
textBox2.Text = turn.ToString();
textBox1.Text = check_winner().ToString();
if (winner == false && turn == 9)
{
textBox1.Text = "Cats Game";
} }

private void button14_Click(object sender, EventArgs e)
{
clear_buttons();
}

private void clear_buttons()
{

button5.Text = "";// Once the player clicks the play again button then all the buttons will clear up to allow the person to play again
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
button10.Text = "";
button11.Text = "";
button12.Text = "";
button13.Text = "";

button5.Enabled = true;// This enables the button to be true once the user presses play again
button6.Enabled = true;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
button10.Enabled = true;
button11.Enabled = true;
button12.Enabled = true;
button13.Enabled = true;

for (int i = 0; i < 3; i++)// This is a for loop
{

for (int j = 0; j < 3; j++)// This is a nesting for loop
{

board[i, j] = 0;// Once the player presses play again the entire board resets and each position becomes 1

}
}

turn = 0;// The turns then resets to 0 to start counting the amount of turns again during a new game

winner = false;// The winner becomes false because since there is a new game there is no new winner

textBox1.Text = winner.ToString();
textBox2.Text = turn.ToString();
}

private void button15_Click(object sender, EventArgs e)
{
textBox3.Text = Owins.ToString();
textBox4.Text = Xwins.ToString();
textBox5.Text = CatsGame.ToString();
}

private void textBox3_TextChanged(object sender, EventArgs e)
{

} }
}

My statistics do not work can someone help me figure out why the X statistics and Cats Game statistics do not work.
 
Physics news on Phys.org
  • #2
Please encase your code in the code tags.
Its impossible to read as it stands.void main()
{
int i =0;
printf("Hello world%d\n", i);
return;
}

Vs

Code:
void main()
{
int i =0;
printf("Hello world%d\n", i);
return;
}
which is easier to read?catsGame is never incremented.

What X stats are you getting??

Also your naming convention for objects should be improved (more as a general rule than a specific problem)
I like naming controls as <abv><Description>
For example cell 0,0 would be named tbCell00
cell 0,1 would be tbCell01

Having everyting TextBox1 TextBox2 doesn't tell you what is what :)
 
  • #3
cpscdave said:
Please encase your code in the code tags.
Its impossible to read as it stands.void main()
{
int i =0;
printf("Hello world%d\n", i);
return;
}

Vs

Code:
void main()
{
int i =0;
printf("Hello world%d\n", i);
return;
}
which is easier to read?catsGame is never incremented.

What X stats are you getting??

For the x stats it keeps getting put under the o stats.
 
  • #4
Trace the execution of the code.
Put a message box popup inside the check winner function.
I'd put one in 3 locations.
Just inside the fuction (so you know that the code when in there)
after the first IF statement
Code:
if (board[0, 0] == 1 && board[0, 1] == 1 && board[0, 2] == 1 || board[0, 0] == 2 && board[0, 1] == 2 && board[0, 2] == 2)

then one each inside the sub if's where you check to see if X or O won. the
Code:
if (board[0, 0] == 1)

The likely culprit is you've written an error in your if statements OR your logic is wrong
 
  • #5
You an also improve the logic of
Code:
if (board[0, 0] == 1 && board[0, 1] == 1 && board[0, 2] == 1 || board[0, 0] == 2 && board[0, 1] == 2 && board[0, 2] == 2)

since you subsequently check to see if 0,0 is 1 or 2 all you care about if the first statement is if the 3 cells are the same. YOu could simplify it to:
Code:
if (board[0, 0] == board[0, 1]  && board[0, 0] == board[0, 2] == 1)
 
  • #6
cpscdave said:
Please encase your code in the code tags.
Its impossible to read as it stands.void main()
{
int i =0;
printf("Hello world%d\n", i);
return;
}

Vs

Code:
void main()
{
int i =0;
printf("Hello world%d\n", i);
return;
}
which is easier to read?catsGame is never incremented.

What X stats are you getting??

Also your naming convention for objects should be improved (more as a general rule than a specific problem)
I like naming controls as <abv><Description>
For example cell 0,0 would be named tbCell00
cell 0,1 would be tbCell01

Having everyting TextBox1 TextBox2 doesn't tell you what is what :)

How do I and where would I increment catsGame?
 
  • #7
Veronica_Oles said:
How do I and where would I increment catsGame?

Where do you think you would??
What condition would prompt you to increment??
 
  • #8
cpscdave said:
Where do you think you would??
What condition would prompt you to increment??
if (board[0, 2] == 1)
{

Xwins++;
}

else
{

Owins++;
}Would I increment it somewhere in my if statements?
 
  • #9
Please use the CODE tags when posting code. Its under the + in the toolbar above where you type :)

Veronica_Oles said:
Would I increment it somewhere in my if statements?

In a TicTacToe game when does a cats game occur??
Where in your code are you checking for that condition? (hint that's where you'd increment it and bonus hint there appears to be multiple places)
 

FAQ: Solve My Tic Tac Toe Program Homework Problem

How does your Tic Tac Toe program work?

My Tic Tac Toe program uses a combination of algorithms and rules to determine the optimal move for the computer player. It first checks for any winning moves and blocks any potential winning moves by the opponent. If there are no immediate winning or blocking moves, it uses a minimax algorithm to analyze all possible future moves and choose the best one.

Can I customize the difficulty level of the computer player?

Yes, my Tic Tac Toe program has a customizable difficulty level. You can choose from easy, medium, or hard difficulty, which will adjust the computer player's strategy and decision-making to provide a suitable challenge for players of different skill levels.

How long did it take you to develop this program?

The development of my Tic Tac Toe program took approximately two weeks. This includes the initial planning and design process, coding, testing, and debugging.

Is your program guaranteed to never lose?

While my Tic Tac Toe program is designed to make optimal moves, it is not guaranteed to never lose. The game of Tic Tac Toe is relatively simple, and it is possible for a skilled human player to defeat the computer player.

Can I use your Tic Tac Toe program for commercial purposes?

Yes, my Tic Tac Toe program is free to use for personal and commercial purposes. However, please ensure to credit the source and include a link to the original program if possible.

Similar threads

Replies
10
Views
2K
Replies
5
Views
2K
Replies
5
Views
5K
Replies
11
Views
9K
Replies
7
Views
2K
Replies
12
Views
2K
Replies
2
Views
1K
Back
Top