Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, my computer science teacher gave us this code for a tictactoe game. What he wants us to do with it is to type in a code into the entire program that will determine the winner of the tictactoe game. The problem is, I don't know where to start.

P.S sorry if the code is so long.

C#
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 Randell_tictactoe
{
    public partial class Form1 : Form
    {
        //This is the code that declares the variables that will be used to create the board
        Rectangle[,] tictactoe;
        int[,] board;
        int player = 0;
        public Form1()
        {
            InitializeComponent();
            board = new int[3, 3];
            tictactoe = new Rectangle[3, 3];
            int i = 0;
            int j = 0;
            while (i <= 2)
            {
                while (j <= 2)
                {
                    board[i, j] = 0;
                    j++;
                }
                j = 0;
                i++;
            }

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //This code will be in charge of painting the tictactoe grid
            int x, y;
            x = tictactoe.GetLength(0);
            y = tictactoe.GetLength(1);
            Pen pn = new Pen(Color.Black, 2);
            Graphics g = e.Graphics;
            int i = 0;
            int j = 0;
            int a, b;
            a = ClientRectangle.Width;
            b = ClientRectangle.Height;
            int storex, storey;
            while (i <= x - 1)
            {
                while (j <= y - 1)
                {

                    tictactoe[i, j] = new Rectangle(i * (a / 3), j * (b / 3), a / 3, b / 3);
                    g.DrawRectangle(pn, tictactoe[i, j]);


                    j++;
                }
                j = 0;
                i++;
            }
            i = 0;
            j = 0;

            while (i <= x - 1)
            {
                while (j <= y - 1)
                {
                    if (board[i, j] == 1)
                    {
                        storex = tictactoe[i, j].X;
                        storey = tictactoe[i, j].Y;
                        g.DrawLine(pn, storex, storey, storex + (a / 3), storey + b / 3);
                        g.DrawLine(pn, storex, storey + b / 3, storex + (a / 3), storey);
                    }
                    else if (board[i, j] == 2)
                    {
                        g.DrawEllipse(pn, tictactoe[i, j].X, tictactoe[i, j].Y, a / 3, b / 3);
                    }
                    j++;
                }
                j = 0;
                i++;
            }
            ResizeRedraw = true;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            //This code is used to check whether the space the player clicks on is occupied by the (human)opponent or not.
            int i = 0;
            int j = 0;
            while (i <= 2)
            {
                while (j <= 2)
                {
                    if (board[i, j] == 0 && player == 0&& tictactoe[i,j].Contains(e.X,e.Y))
                    {
                        board[i, j] = 1;
                        player = 1;
                        Invalidate();
                    }
                    else if (board[i, j] == 0 && player == 1 & tictactoe[i, j].Contains(e.X, e.Y))
                    {
                        board[i, j] = 2;
                        player = 0;
                        Invalidate();
                    }
                    j++;
                }
                j = 0;
                i++;
            }
        }
    }
}
Posted
Comments
ZurdoDev 8-May-14 13:45pm    
First step is to go through the code and figure out what it is doing.
[no name] 8-May-14 18:50pm    
If you don't know where to start then you need to go back to your teacher and get them to earn their pay.
Emre Ataseven 10-May-14 9:33am    
Does it work?

This is homework which is set to help YOU to learn, which means we are unlikely to give you much help unless you get stuck on a particular point.

I'm sure your teacher must have given you some clues as to how to proceed, so have a go first and come back if you get stuck on a particular problem.

Failing that try searching for "c# tic tac toe" ... there is actually an article here on CodeProject that would help
 
Share this answer
 
C#
private bool AllCellsFull()
{
    for (int i = 0; i <= 2; i++)
    {
        for (int j = 0; j <= 2; j++)
        {
            if (board[i, j] == 0)
                return false;
        }
    }
    return true;
}

private string GetWinner()
{
    for (int i = 0; i <= 2; i++)
    {
        var check1 = Enumerable.Range(0, 3).Select(p => board[p, i]);

        if (!check1.Contains(0) && check1.Distinct().Count() == 1)
            return check1.ElementAt(0) == 1 ? "X" : "O";

        var check2 = Enumerable.Range(0, 3).Select(p => board[i, p]);
        if (!check2.Contains(0) && check2.Distinct().Count() == 1)
            return check2.ElementAt(0) == 1 ? "X" : "O";
    }

    var check3 = Enumerable.Range(0, 3).Select(p => board[p, p]);
    if (!check3.Contains(0) && check3.Distinct().Count() == 1)
        return check3.ElementAt(0) == 1 ? "X" : "O";

    var check4 = Enumerable.Range(0, 3).Select(p => board[p, 2 - p]);
    if (!check4.Contains(0) && check4.Distinct().Count() == 1)
        return check4.ElementAt(0) == 1 ? "X" : "O";

    return "Draw";
}



Then at the end of your mouse down event;

C#
...
...
           string result = GetWinner();
           if (AllCellsFull() || result != "Draw")
               MessageBox.Show("Game Result: " + result);
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900