1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Tic Tac Toe Program

  1. Jun 2, 2016 #1
    1. The problem statement, all variables and given/known data
    Making tic tac toe program!

    2. Relevant equations


    3. 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 theres 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.
     
  2. jcsd
  3. Jun 2, 2016 #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 (C):
    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 :)
     
  4. Jun 2, 2016 #3
    For the x stats it keeps getting put under the o stats.
     
  5. Jun 2, 2016 #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 (C):
    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 (C):
    if (board[0, 0] == 1)
    The likely culprit is you've written an error in your if statements OR your logic is wrong
     
  6. Jun 2, 2016 #5
    You an also improve the logic of
    Code (C):
    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 (C):
    if (board[0, 0] == board[0, 1]  && board[0, 0] == board[0, 2] == 1)
     
  7. Jun 2, 2016 #6
    How do I and where would I increment catsGame?
     
  8. Jun 2, 2016 #7
    Where do you think you would??
    What condition would prompt you to increment??
     
  9. Jun 2, 2016 #8
    if (board[0, 2] == 1)
    {

    Xwins++;
    }

    else
    {

    Owins++;



    }


    Would I increment it somewhere in my if statements?
     
  10. Jun 2, 2016 #9
    Please use the CODE tags when posting code. Its under the + in the toolbar above where you type :)

    In a TicTacToe game when does a cats game occur??
    Where in your code are you checking for that condition? (hint thats where you'd increment it and bonus hint there appears to be multiple places)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Tic Tac Toe Program
  1. Use of tic toc in MATLAB (Replies: 12)

Loading...