Solve My Tic Tac Toe Program Homework Problem

  • Thread starter Thread starter Veronica_Oles
  • Start date Start date
  • Tags Tags
    Program Toe
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the creation of a Tic Tac Toe program using C#. Participants are sharing code snippets, discussing the logic for player turns, win conditions, and the overall structure of the program.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant outlines the structure of the program, including the use of a 2D array to represent the game board and variables to track turns and wins.
  • There is a detailed explanation of how player turns are managed, with alternating moves between 'O' and 'X' based on the turn count.
  • Participants discuss the implementation of the win-checking logic, which evaluates rows, columns, and diagonals to determine if a player has won.
  • Some participants express uncertainty about the handling of the game state, particularly regarding the conditions under which the game ends in a tie, referred to as "Cats Game."
  • There are multiple methods defined for handling button clicks, each corresponding to a position on the Tic Tac Toe board, which raises questions about code efficiency and redundancy.

Areas of Agreement / Disagreement

Participants appear to agree on the basic structure of the program and the general approach to handling turns and win conditions. However, there is no consensus on the efficiency of the code or the best practices for implementing the game logic, indicating ongoing debate and exploration of different coding strategies.

Contextual Notes

Some participants note potential issues with the win-checking logic, such as the need for clearer handling of the winner variable and the implications of the game state after nine turns. There are also concerns about the redundancy in the button click methods, suggesting a need for refactoring.

Veronica_Oles
Messages
141
Reaction score
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
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 :)
 
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.
 
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
 
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)
 
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?
 
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??
 
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?
 
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)
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
10K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K