0

I have run into a few issues with a program of mine. It is a school assignment and I've gotten most of the code down but I'm running into compiler errors. I am to create a tic tac toe simulator (no human players) track the game with a 2d array and then delcare a winner. I had my code running ok but then started to break things up into differnet objects and I think I've messed it up. I keep getting comiler errors that say:

Error   1   'System.Drawing.Point' does not contain a definition for 'x' and no extension method 'x' accepting a first argument of type 'System.Drawing.Point' could be found (are you missing a using directive or an assembly reference?) C:\Users\documents\visual studio 2013\Projects\Unit7\Unit7\Form1.cs 119 23  Unit7
Error   3   'System.Drawing.Point' does not contain a definition for 'x' and no extension method 'x' accepting a first argument of type 'System.Drawing.Point' could be found (are you missing a using directive or an assembly reference?) C:\Users\documents\visual studio 2013\Projects\Unit7\Unit7\Form1.cs 128 35Unit7
Error   2   'System.Drawing.Point' does not contain a definition for 'y' and no extension method 'y' accepting a first argument of type 'System.Drawing.Point' could be found (are you missing a using directive or an assembly reference?) C:\Users\documents\visual studio 2013\Projects\Unit7\Unit7\Form1.cs 120 23  Unit7
Error   7   Cannot convert null to 'System.Drawing.Point' because it is a non-nullable value type   C:\Users\documents\visual studio 2013\Projects\Unit7\Unit7\Form1.cs 139 20  Unit7
Error   4   The name 'gameBoard' does not exist in the current context  C:\Users\documents\visual studio 2013\Projects\Unit7\Unit7\Form1.cs 134 21  Unit7
Error   5   The name 'y' does not exist in the current context  C:\Users\documents\visual studio 2013\Projects\Unit7\Unit7\Form1.cs 134 34  Unit7
Error   6   The name 'y' does not exist in the current context  C:\Users\documents\visual studio 2013\Projects\Unit7\Unit7\Form1.cs 136 41  Unit7

The other thing I have had issues with is that I can't seem to change the size of the x's and o' so that they are bigger and more alligned than they are now, and I would like to have lines that denote the board on screen if i could too but I'm not sure I know how to do that. I searched and with no luck in a way to help this.

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

namespace Kim_Johnson_Unit7
{
    public partial class TTT_frm : Form
    {
        private Random Generated = new Random();
        private Point LastMove;
        private const int Board_Size = 3;
        const int Squares = 9;
        char[] XorO = new char[Squares];

        public TTT_frm()
        {
            InitializeComponent();
        }

        private void TTT_frm_Load(object sender, EventArgs e)
        {

        }

        private void Sim_btn_Click(object sender, EventArgs e)
        {
            //Erase contents of Array
            for (int i = 0; i < Squares; i++)
            {
                XorO[i] = ' ';
            }
            //Erase contents of Results label
            Result_lbl.Text = " ";

            //Set fist and second player X's and O's
            char FirstPlayer = 'O';
            char SecondPlayer = 'X';

            //Generate the random numbers for the grid
            int[] position = new int[5];
            position[0] = Generated.Next(9);
            for (int i = 1; i < 5; i++)
            {
                int temp = Generated.Next(9);
                for (int j = 0; j < i; j++)
                {
                    if (temp == position[j])
                    {
                        i--;
                        break;
                    }
                    else
                    {
                        position[i] = temp;
                    }
                }
            }

            //Get first players positions
            for (int i = 0; i < 5; i++)
            {
                XorO[position[i]] = FirstPlayer;
            }

            for (int i = 0; i < Squares; i++)
            {
                if (XorO[i] != FirstPlayer)
                {
                    XorO[i] = SecondPlayer;
                }
            }

            //Put the X's or O's in the labels
            A1_lbl.Text = XorO[0].ToString();
            A2_lbl.Text = XorO[1].ToString();
            A3_lbl.Text = XorO[2].ToString();
            B1_lbl.Text = XorO[3].ToString();
            B2_lbl.Text = XorO[4].ToString();
            B3_lbl.Text = XorO[5].ToString();
            C1_lbl.Text = XorO[6].ToString();
            C2_lbl.Text = XorO[7].ToString();
            C3_lbl.Text = XorO[8].ToString();

            //See Who Won
            switch(Winner())
            {
                case 'O':
                    Result_lbl.Text = "O Wins!";
                    break;
                case 'x':
                    Result_lbl.Text = "x Wins!";
                    break;
                case 'T':
                    Result_lbl.Text = "It Was a Tie!";
                    break;
             }            
        }

        private void GenerateNextStep(char playerChar, char[][] gameBoard)
        {
            int x;
            int y;

            if (LastMove ==null)
            {
                //Is the first move and can place anywhere
                x = Generated.Next() % Board_Size;
                y = Generated.Next() % Board_Size;
            }
            else
            {
                Point p = FindClosest();
                x = p.x;
                y = p.y;
            }
            gameBoard[x][y] = playerChar;
            LastMove = new Point(x, y);
        }

        private Point FindClosest()
        {
            for (int x = LastMove.x - 1; x < x +1; x++)
            {
                if (x < 0 || x >= Board_Size)
                {
                    continue;
                }
                if (gameBoard[x][y] == ' ')
                {
                    return new Point(x, y);
                }
            }
            return null;
        }


        private char Winner()
        {
            //Return result of game so that a determination can be made
            //Returns X, O or T
            char winner = ' ';
            int Win_Sequence = 0;

            //Columns
            if (XorO[0].Equals(XorO[3]) && XorO[0].Equals(XorO[6]))
            {
                Win_Sequence++;
                winner = XorO[0];
            }
            if (XorO[1].Equals(XorO[4]) && XorO[1].Equals(XorO[7]))
            {
                Win_Sequence++;
                winner = XorO[1];
            }
            if (XorO[2].Equals(XorO[5]) && XorO[2].Equals(XorO[8]))
            {
                Win_Sequence++;
                winner = XorO[2];
            }

            //Rows
            if (XorO[0].Equals(XorO[1]) && XorO[0].Equals(XorO[2]))
            {
                Win_Sequence++;
                winner = XorO[0];
            }
            if (XorO[3].Equals(XorO[4]) && XorO[3].Equals(XorO[5]))
            {
                Win_Sequence++;
                winner = XorO[3];
            }
            if (XorO[6].Equals(XorO[7]) && XorO[6].Equals(XorO[8]))
            {
                Win_Sequence++;
                winner = XorO[6];
            }

            //Diagonal
            if (XorO[0].Equals(XorO[4]) && XorO[0].Equals(XorO[8]))
            {
                Win_Sequence++;
                winner = XorO[0];
            }
            if (XorO[2].Equals(XorO[4]) && XorO[2].Equals(XorO[8]))
            {
                Win_Sequence++;
                winner = XorO[2];
            }

            if (Win_Sequence == 0 || Win_Sequence > 1)
            {
                winner = 'T';
            }

            return winner;
        }

        private void Exit_btn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
4
Contributors
3
Replies
13
Views
1 Year
Discussion Span
Last Post by JOSheaIV
1

In here:

            x = p.x;
            y = p.y;

The Point properties are X and Y, not x and y.

In here:

        private Point FindClosest()
        {
           for (int x = LastMove.x - 1; x < x +1; x++)
            {
                if (x < 0 || x >= Board_Size)
                {
                    continue;
                }
                if (gameBoard[x][y] == ' ')
                {
                    return new Point(x, y);
                }
            }
            return null;
        }

A Point object is not nullable, therefore you can't return null. You could try

return new Point();

Edited by tinstaafl

0

If you would like to draw grid lines, and easily change the size of the Xs and Os, I would suggest maybe using a different Windows Form Component to something along the lines of a DataGridView. Back in college we had to develop a Sudoku game, and this was the component I used for our project.

What's also nice about that is you can techically do something like dataGridView1.Rows[0].Cells[0].Value.ToString() to get the value of the grid piece, or if you take off the Value piece, you'll see there are other options you can do to interact with the cell. I bring this up because if you were to use this, it would be easier to use just the DataGridView, and not try and have a 2D array as well as the DataGridView (unless you continue to generate it ahead of time, but then you just index through and populate the DataGridView)

Edited by JOSheaIV

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.