11 Replies - 615 Views - Last Post: 21 January 2013 - 04:05 PM Rate Topic: -----

#1 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Finished "playable" tic tac toe but few problems occuring

Posted 18 January 2013 - 11:00 AM

hi all , so from my learning book i had to create a tic tac toe game ( not using visualC# buttons etc are next chapter hence my bad attempt for reset button)

a. for an easier version, let the computer make its move in any available square
(i did this part not to do with this question)

b. For a more challenging program, have the computer find its best move.

now straight away i started looking on internet and found minimax thoery with alpha-beta but whether i just cant understand the online tutorials examples or seeing things i havent learnt yet only makes me think i will learn the parts im missing later in the book?

here are my problems,

i managed to get the game to check if user or player can win in next move and stop this but maybe due to way i have wrote the loops i cannot see a way of prioritizing going for the win over stopping user winning for example

X | | X
. | X |
O | | O

due to the checkwin order it will pick up the X(user) defend rather than take the O(AI) win?


also when either isnt going to win next move , i have it so the computer generates random number then sees if its available if so fill space in but i guess this is where minimax would tell it the "best" position to go?

here is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

public class TicTacToe : Form
{
    int maxturn;
    int[] squareempty = new int[9];
    int index = 10;
    int endofgame = 0;
    int whosturn; // 0 for comp 1 for user
    int[] wongame = new int[9];
    // button for new game
    // create the boxes
    /////////////////////////////////////
    //resetbutton
    GraphicsPath reset = new GraphicsPath();
    Region resetregion = new Region();
    /// /////////////////////////////////
    // array rectangles
    //RectangleF[][] rectanglepoints = new RectangleF[9][];
    GraphicsPath[] grid = new GraphicsPath[9];
    Region[] gridregion = new Region[9];
    ////////////////////////////////////////
    //String X props
    GraphicsPath[] xicons = new GraphicsPath[9];
    Region[] xiconregion = new Region[9];
    /// <summary>
    /// array for x and o values
    /// </summary>
    int[] XOvalues = new int[9];
    // array for checked boxes
    int[] checkedboxes = new int[9];
    Random Turn = new Random();
    Random r = new Random();
    int[] oldX = new int[9];
    int[] oldY = new int[9];
    bool check1 = false;
    bool check2 = false;
    bool check3 = false;
    bool check4 = false;
    bool check5 = false;
    bool check6 = false;
    bool check7 = false;
    bool check8 = false;
    bool check9 = false;
    bool check10 = false;
    bool check11 = false;
    bool check12 = false;
    bool check13 = false;
    bool check14 = false;
    bool check15 = false;
    bool check16 = false;
    bool check17 = false;
    bool check18 = false;
    bool check19 = false;
    bool check20 = false;
    bool check21 = false;
    bool check22 = false;
    bool check23 = false;
    bool check24 = false;
    bool firstturn = true;

    public TicTacToe()
    {
        Size = new Size(800, 800);
        Text = "Tic Tac Toe";
        BackColor = Color.Red;
        int w = DisplayRectangle.Width - 20;
        int h = DisplayRectangle.Height - 20;
        whosturn = Turn.Next(2);
        if (whosturn == 1)
        { firstturn = false; }

        //////////////////////////
        // reset button
        Point resetpoint = new Point(w / 10, h / 10 * 9);
        reset.AddString("Reset", FontFamily.GenericSerif, 0, 60, resetpoint, StringFormat.GenericDefault);
        resetregion = new Region(reset);

        //////////////////////////
        // Array of rectangles
        // 100, h / 50, 150, 50




    }

    public void Initboard(Graphics g)
    {
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;

        int hofrect = h / 20 * 5;
        int sizeofsquare = w / 3;
        RectangleF[] rectanglepoints = { new RectangleF(w / 20+5, h / 20+5, w / 20 * 6 - 10, h/20 * 5-10), new RectangleF(w / 20 * 7, h / 20+5, w / 20 * 6 - 5, h / 20 * 5-10), new RectangleF(w / 20*13, h / 20+5, w / 20 * 6-5, h / 20 * 5-10), 
                                         new RectangleF(w / 20+5, h / 20*6, w / 20 * 6 - 10, h/20*5-5), new RectangleF(w / 20 * 7, h / 20*6, w / 20 * 6 - 5, h / 20 * 5-5), new RectangleF(w / 20 * 13, h / 20*6, w / 20 * 6-5, h / 20 * 5-5), 
                                       new RectangleF(w / 20+5, h / 20*11, w / 20 * 6 - 10, h/20*5-5), new RectangleF(w / 20 * 7, h/20*11, w / 20 * 6 - 5, h / 20 * 5-5), new RectangleF(w / 20 * 13, h/20*11, w / 20 * 6-5, h / 20 * 5-5)
                                       };
        for (int i = 0; i < rectanglepoints.Length; i++)
        {
            grid[i] = new GraphicsPath();
            grid[i].AddRectangle(rectanglepoints[i]);
            gridregion[i] = new Region(grid[i]);

        }
        Pen outline = new Pen(Color.White, 5);
        //reset button
        g.FillRegion(Brushes.White, resetregion);
        g.DrawRectangle(outline, w / 10, h / 10 * 9 - 20, w / 10 * 2, h / 10 + 10);
        g.DrawRectangle(Pens.Black, w / 10, h / 10 * 9 - 20, w / 10 * 2, h / 10 + 10);
        g.DrawRectangle(Pens.Black, w / 10 - 3, h / 10 * 9 - 23, w / 10 * 2 + 5, h / 10 + 15);
        g.DrawRectangle(Pens.Black, w / 10 + 3, h / 10 * 9 - 17, w / 10 * 2 - 5, h / 10 + 5);
        ////////////////////////////////////////////////
        // Xicons
        RectangleF[] xiconpoints = { new RectangleF(w / 20+5, h / 20+5, w / 20 * 6 - 10, h/20 * 5-10), new RectangleF(w / 20 * 7, h / 20+5, w / 20 * 6 - 5, h / 20 * 5-10), new RectangleF(w / 20*13, h / 20+5, w / 20 * 6-5, h / 20 * 5-10), 
                                         new RectangleF(w / 20+5, h / 20*6, w / 20 * 6 - 10, h/20*5-5), new RectangleF(w / 20 * 7, h / 20*6, w / 20 * 6 - 5, h / 20 * 5-5), new RectangleF(w / 20 * 13, h / 20*6, w / 20 * 6-5, h / 20 * 5-5), 
                                       new RectangleF(w / 20+5, h / 20*11, w / 20 * 6 - 10, h/20*5-5), new RectangleF(w / 20 * 7, h/20*11, w / 20 * 6 - 5, h / 20 * 5-5), new RectangleF(w / 20 * 13, h/20*11, w / 20 * 6-5, h / 20 * 5-5)
                                   };
        PointF newpoint = new PointF(100, 100);
        for (int x = 0; x < 1; x++)
        {
            xicons[x] = new GraphicsPath();
            xicons[x].AddString("X", FontFamily.GenericSerif, 1, 100, newpoint, StringFormat.GenericDefault);
            xiconregion[x] = new Region(xicons[x]);
        }

        //white background for game
        g.FillRectangle(Brushes.White, w / 20, h / 20, w / 20 * 18, h / 20 * 15);
        // grid outlines
        Pen Blackline = new Pen(Brushes.Black, 5);
        g.DrawLine(Blackline, w / 20 - 1, h / 20 + 2, w / 20 * 19, h / 20 + 2);
        g.DrawLine(Blackline, w / 20 - 1, h / 20 * 16 - 3, w / 20 * 19, h / 20 * 16 - 3);
        g.DrawLine(Blackline, w / 20 * 19 - 3, h / 20, w / 20 * 19 - 3, h / 20 * 16 - 3);
        g.DrawLine(Blackline, w / 20 + 2, h / 20 * 16 - 3, w / 20 + 2, h / 20);
        g.DrawLine(Blackline, w / 20 * 7 - 3, h / 20 * 16 - 3, w / 20 * 7 - 3, h / 20);
        g.DrawLine(Blackline, w / 20 * 13 - 3, h / 20 * 16 - 3, w / 20 * 13 - 3, h / 20);
        g.DrawLine(Blackline, w / 20 - 1, h / 20 * 6 - 3, w / 20 * 19, h / 20 * 6 - 3);
        g.DrawLine(Blackline, w / 20 - 1, h / 20 * 11 - 3, w / 20 * 19, h / 20 * 11 - 3);
    }

    protected override void OnPaint(PaintEventArgs e)
    {

        Graphics g = e.Graphics;
        Initboard(g);
        CheckForWin(g);
        ArrayScores(g);
        AiDefendorWinMove();
        AiBestMove();

        base.OnPaint(e);
    }


    public void AiDefendorWinMove()
    {
        if (endofgame == 0 && whosturn == 0)
        {

            //while (whosturn == 0)
            //{

            if (whosturn == 0 && firstturn == true)
            {
                int firstgo = r.Next(9);
                index = firstgo;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                firstturn = false;
                maxturn++;
                Invalidate();

            }



            //check to stop player winning , loops below checks first two columns 

            if (XOvalues[0] == XOvalues[1] && XOvalues[2] == 0 && check1 == false && whosturn == 0 && XOvalues[0] != 0)
            {
                index = 2;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check1 = true;
                maxturn++;
                Invalidate();

            }
            if (XOvalues[3] == XOvalues[4] && XOvalues[5] == 0 && check2 == false && whosturn == 0 && XOvalues[3] != 0)
            {
                index = 5;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check2 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[6] == XOvalues[7] && XOvalues[8] == 0 && check3 == false && whosturn == 0 && XOvalues[6] != 0)
            {
                index = 8;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check3 = true;
                maxturn++;
                Invalidate();

            }


            //check to stop player winning , loops below checks last two columns 

            if (XOvalues[1] == XOvalues[2] && XOvalues[0] == 0 && check4 == false && whosturn == 0 && XOvalues[1] != 0)
            {
                index = 0;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check4 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[4] == XOvalues[5] && XOvalues[3] == 0 && check5 == false && whosturn == 0 && XOvalues[4] != 0)
            {
                index = 3;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check5 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[7] == XOvalues[8] && XOvalues[6] == 0 && check6 == false && whosturn == 0 && XOvalues[7] != 0)
            {
                index = 6;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check6 = true;
                maxturn++;
                Invalidate();
            }
            //check to stop player winning , loops below checks first and last columns 

            if (XOvalues[0] == XOvalues[2] && XOvalues[1] == 0 && check7 == false && whosturn == 0 && XOvalues[0] != 0)
            {
                index = 1;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check7 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[3] == XOvalues[5] && XOvalues[4] == 0 && check8 == false && whosturn == 0 && XOvalues[3] != 0)
            {
                index = 4;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check8 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[6] == XOvalues[8] && XOvalues[7] == 0 && check9 == false && whosturn == 0 && XOvalues[6] != 0)
            {
                index = 7;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check9 = true;
                maxturn++;
                Invalidate();
            }
            //check to stop player winning , loops below checks first 2 rows 

            if (XOvalues[0] == XOvalues[3] && XOvalues[6] == 0 && check10 == false && whosturn == 0 && XOvalues[0] != 0)
            {
                index = 6;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check10 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[1] == XOvalues[4] && XOvalues[7] == 0 && check11 == false && whosturn == 0 && XOvalues[1] != 0)
            {
                index = 7;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check11 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[2] == XOvalues[5] && XOvalues[8] == 0 && check12 == false && whosturn == 0 && XOvalues[2] != 0)
            {
                index = 8;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check12 = true;
                maxturn++;
                Invalidate();
            }
            //check to stop player winning , loops below checks last 2 rows 

            if (XOvalues[3] == XOvalues[6] && XOvalues[0] == 0 && check13 == false && whosturn == 0 && XOvalues[3] != 0)
            {
                index = 0;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check13 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[4] == XOvalues[7] && XOvalues[1] == 0 && check14 == false && whosturn == 0 && XOvalues[4] != 0)
            {
                index = 1;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check14 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[5] == XOvalues[8] && XOvalues[2] == 0 && check15 == false && whosturn == 0 && XOvalues[5] != 0)
            {
                index = 2;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check15 = true;
                maxturn++;
                Invalidate();
            }
            //check to stop player winning , loops below checks first and last rows 

            if (XOvalues[0] == XOvalues[6] && XOvalues[3] == 0 && check16 == false && whosturn == 0 && XOvalues[0] != 0)
            {
                index = 3;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check16 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[1] == XOvalues[7] && XOvalues[4] == 0 && check17 == false && whosturn == 0 && XOvalues[1] != 0)
            {
                index = 4;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check17 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[2] == XOvalues[8] && XOvalues[5] == 0 && check18 == false && whosturn == 0 && XOvalues[2] != 0)
            {
                index = 5;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check18 = true;
                maxturn++;
                Invalidate();
            }
            //check to stop player winning , loops below checks diagonal wins  

            if (XOvalues[0] == XOvalues[4] && XOvalues[8] == 0 && check19 == false && whosturn == 0 && XOvalues[0] != 0)
            {
                index = 8;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check19 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[4] == XOvalues[8] && XOvalues[0] == 0 && check20 == false && whosturn == 0 && XOvalues[4] != 0)
            {
                index = 0;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check20 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[0] == XOvalues[8] && XOvalues[4] == 0 && check21 == false && whosturn == 0 && XOvalues[0] != 0)
            {
                index = 4;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check21 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[2] == XOvalues[4] && XOvalues[6] == 0 && check22 == false && whosturn == 0 && XOvalues[2] != 0)
            {
                index = 6;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check22 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[6] == XOvalues[4] && XOvalues[2] == 0 && check23 == false && whosturn == 0 && XOvalues[6] != 0)
            {
                index = 2;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check23 = true;
                maxturn++;
                Invalidate();
            }
            if (XOvalues[2] == XOvalues[6] && XOvalues[4] == 0 && check24 == false && whosturn == 0 && XOvalues[6] != 0)
            {
                index = 4;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                check24 = true;
                maxturn++;
                Invalidate();
            }
            // whosturn = 1;

            // maxturn++;

            // }
        }
    }
    public void AiBestMove()
    {
        if (endofgame == 0)
        {

            if (whosturn == 0)
            {
                int Bestmove = r.Next(9);

                for (int i = 0; i < 100; i++)
                {
                    if (XOvalues[Bestmove] == 0)
                    {
                        index = Bestmove;
                        XOvalues[index] = 5;
                        checkedboxes[index] = index;
                        wongame[index] = 5;
                        whosturn = 1;
                        maxturn++;
                        Invalidate();
                        break;
                    }
                    else
                    {
                        Bestmove = r.Next(9);
                    }
                }
            }

        }
    }
    public void CheckForWin(Graphics g)
    {
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;
        // See if player won
        String Userwins = "User Wins!";
        String CompWins = "Computer Wins!";
        
        Font winfont = new Font("Arial", w / 20, FontStyle.Regular);
        int strCompWin = (int)g.MeasureString(CompWins,winfont).Width;
        int StrCompWinPlace = w - strCompWin / 2;
        Font wincompfont = new Font("Arial", 30, FontStyle.Regular);


        int win1 = (wongame[0] + wongame[1] + wongame[2]);
        int win2 = (wongame[3] + wongame[4] + wongame[5]);
        int win3 = (wongame[6] + wongame[7] + wongame[8]);
        int win4 = (wongame[0] + wongame[3] + wongame[6]);
        int win5 = (wongame[1] + wongame[4] + wongame[7]);
        int win6 = (wongame[2] + wongame[5] + wongame[8]);
        int win7 = (wongame[0] + wongame[4] + wongame[8]);
        int win8 = (wongame[2] + wongame[4] + wongame[6]);


        if (win1 == 6)
        {
            g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17);
            g.FillRegion(Brushes.Red, gridregion[0]);
            g.FillRegion(Brushes.Red, gridregion[1]);
            g.FillRegion(Brushes.Red, gridregion[2]);
            endofgame = 1;

        }
        if (win2 == 6)
        {
            g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17);
            g.FillRegion(Brushes.Red, gridregion[3]);
            g.FillRegion(Brushes.Red, gridregion[4]);
            g.FillRegion(Brushes.Red, gridregion[5]);
            endofgame = 1;

        }
        if (win3 == 6)
        {
            g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17);
            g.FillRegion(Brushes.Red, gridregion[6]);
            g.FillRegion(Brushes.Red, gridregion[7]);
            g.FillRegion(Brushes.Red, gridregion[8]);
            endofgame = 1;
        }
        if (win4 == 6)
        {
            g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17);
            g.FillRegion(Brushes.Red, gridregion[0]);
            g.FillRegion(Brushes.Red, gridregion[3]);
            g.FillRegion(Brushes.Red, gridregion[6]);
            endofgame = 1;
        }
        if (win5 == 6)
        {
            g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17);
            g.FillRegion(Brushes.Red, gridregion[1]);
            g.FillRegion(Brushes.Red, gridregion[4]);
            g.FillRegion(Brushes.Red, gridregion[7]);
            endofgame = 1;
        }
        if (win6 == 6)
        {
            g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17);
            g.FillRegion(Brushes.Red, gridregion[2]);
            g.FillRegion(Brushes.Red, gridregion[5]);
            g.FillRegion(Brushes.Red, gridregion[8]);
            endofgame = 1;
        }

        if (win7 == 6)
        {
            g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17);
            g.FillRegion(Brushes.Red, gridregion[0]);
            g.FillRegion(Brushes.Red, gridregion[4]);
            g.FillRegion(Brushes.Red, gridregion[8]);
            endofgame = 1;
        }

        if (win8 == 6)
        {
            g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17);
            g.FillRegion(Brushes.Red, gridregion[2]);
            g.FillRegion(Brushes.Red, gridregion[4]);
            g.FillRegion(Brushes.Red, gridregion[6]);
            endofgame = 1;
        }



        if (win1 == 15)
        {
            g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10);
            g.FillRegion(Brushes.Red, gridregion[0]);
            g.FillRegion(Brushes.Red, gridregion[1]);
            g.FillRegion(Brushes.Red, gridregion[2]);
            endofgame = 1;

        }
        if (win2 == 15)
        {
            g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10);
            g.FillRegion(Brushes.Red, gridregion[3]);
            g.FillRegion(Brushes.Red, gridregion[4]);
            g.FillRegion(Brushes.Red, gridregion[5]);
            endofgame = 1;

        }
        if (win3 == 15)
        {
            g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10);
            g.FillRegion(Brushes.Red, gridregion[6]);
            g.FillRegion(Brushes.Red, gridregion[7]);
            g.FillRegion(Brushes.Red, gridregion[8]);
            endofgame = 1;
        }
        if (win4 == 15)
        {
            g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10);
            g.FillRegion(Brushes.Red, gridregion[0]);
            g.FillRegion(Brushes.Red, gridregion[3]);
            g.FillRegion(Brushes.Red, gridregion[6]);
            endofgame = 1;
        }
        if (win5 == 15)
        {
            g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17+ 10);
            g.FillRegion(Brushes.Red, gridregion[1]);
            g.FillRegion(Brushes.Red, gridregion[4]);
            endofgame = 1;
            g.FillRegion(Brushes.Red, gridregion[7]);

        }
        if (win6 == 15)
        {
            g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10);
            g.FillRegion(Brushes.Red, gridregion[2]);
            g.FillRegion(Brushes.Red, gridregion[5]);
            g.FillRegion(Brushes.Red, gridregion[8]);
            endofgame = 1;
        }

        if (win7 == 15)
        {
            g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10);
            g.FillRegion(Brushes.Red, gridregion[0]);
            g.FillRegion(Brushes.Red, gridregion[4]);
            g.FillRegion(Brushes.Red, gridregion[8]);
            endofgame = 1;
        }

        if (win8 == 15)
        {
            g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10);
            g.FillRegion(Brushes.Red, gridregion[2]);
            g.FillRegion(Brushes.Red, gridregion[4]);
            g.FillRegion(Brushes.Red, gridregion[6]);
            endofgame = 1;
        }



        // see if there is draw

        if (maxturn == 9)
        {
            if (endofgame == 0)
            {
                g.DrawString("Draw!", winfont, Brushes.Black, w / 20 * 10, h / 20 * 17);
            }

        }
    }
    public void ArrayScores(Graphics g)
    {

        ////////////////////////////////////////////////
        // create array - if userturn then value = 2, if comp turn then value = 4, 
        // if loop - if array == 2 draw in X if array ==4 draw in O


        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;
        int fontsize = (w / 20 * 3);
        Font arial = new Font("Arial", fontsize, FontStyle.Regular);
        for (int x = 0; x < 9; x++)
        {
            if (XOvalues[x] == 2)
            {

                if (checkedboxes[x] == 0 && XOvalues[x] == 2)
                {
                    g.DrawString("X", arial, Brushes.Black, w / 20 + 15, h / 20 - 10, StringFormat.GenericDefault);
                    squareempty[x] = 1;
                }
                else if (checkedboxes[x] == 1 && XOvalues[x] == 2)
                {
                    g.DrawString("X", arial, Brushes.Black, w / 20 * 7 + 15, h / 20 - 10, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 2 && XOvalues[x] == 2)
                {
                    g.DrawString("X", arial, Brushes.Black, w / 20 * 13 + 15, h / 20 - 10, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 3 && XOvalues[x] == 2)
                {
                    g.DrawString("X", arial, Brushes.Black, w / 20 + 20, h / 20 * 6 - 10, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 4 && XOvalues[x] == 2)
                {
                    g.DrawString("X", arial, Brushes.Black, w / 20 * 7 + 20, h / 20 * 6 - 10, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 5 && XOvalues[x] == 2)
                {
                    g.DrawString("X", arial, Brushes.Black, w / 20 * 13 + 15, h / 20 * 6 - 10, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 6 && XOvalues[x] == 2)
                {
                    g.DrawString("X", arial, Brushes.Black, w / 20 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 7 && XOvalues[x] == 2)
                {
                    g.DrawString("X", arial, Brushes.Black, w / 20 * 7 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 8 && XOvalues[x] == 2)
                {
                    g.DrawString("X", arial, Brushes.Black, w / 20 * 13 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault);

                }
            }

            else if (XOvalues[x] == 5)
            {
                if (checkedboxes[x] == 0 && XOvalues[x] == 5)
                {
                    g.DrawString("O", arial, Brushes.Black, w / 20 + 15, h / 20 - 10, StringFormat.GenericDefault);
                    squareempty[x] = 1;
                }
                else if (checkedboxes[x] == 1 && XOvalues[x] == 5)
                {
                    g.DrawString("O", arial, Brushes.Black, w / 20 * 7 + 15, h / 20 - 10, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 2 && XOvalues[x] == 5)
                {
                    g.DrawString("O", arial, Brushes.Black, w / 20 * 13 + 15, h / 20 - 10, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 3 && XOvalues[x] == 5)
                {
                    g.DrawString("O", arial, Brushes.Black, w / 20 + 20, h / 20 * 6 - 10, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 4 && XOvalues[x] == 5)
                {
                    g.DrawString("O", arial, Brushes.Black, w / 20 * 7 + 20, h / 20 * 6 - 10, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 5 && XOvalues[x] == 5)
                {
                    g.DrawString("O", arial, Brushes.Black, w / 20 * 13 + 15, h / 20 * 6 - 10, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 6 && XOvalues[x] == 5)
                {
                    g.DrawString("O", arial, Brushes.Black, w / 20 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 7 && XOvalues[x] == 5)
                {
                    g.DrawString("O", arial, Brushes.Black, w / 20 * 7 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault);

                }
                else if (checkedboxes[x] == 8 && XOvalues[x] == 5)
                {
                    g.DrawString("O", arial, Brushes.Black, w / 20 * 13 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault);

                }
            }

        }
    }
    protected override void onmousedown(MouseEventArgs e)
    {
        ////////////////////////////////////
        // reset button
        /////////////////////////////////////
        RectangleF resetrect = resetregion.GetBounds(CreateGraphics());

        if (resetrect.Contains(e.X, e.Y))
        {
            //start = true;
            //Invalidate();
            Application.Restart();
            

        }

        /////////////////////////////////////
        // click in squares
        ////////////////////////////////////

        Boolean found = false;
        int i = 0;

        while (!found && i < gridregion.Length && endofgame == 0)
        {
            RectangleF[] scans = gridregion[i].GetRegionScans(new Matrix());
            int j = 0;
            while (!found && j < scans.Length)
            {
                if (scans[j].Contains(e.X, e.Y) && XOvalues[i] == 0)
                {

                    index = i;

                    found = true;


                }
                j++;
            }
            i++;
        }
        /////////////////////////////////////////////

        // Invalidate();
        base.onmousedown(e);
    }

    protected override void onmouseup(MouseEventArgs e)
    {
        if (endofgame == 0)
        {
            if (index < 10 && whosturn == 1 && XOvalues[index] == 0)
            {
                XOvalues[index] = 2;
                checkedboxes[index] = index;
                wongame[index] = 2;
                whosturn = 0;
                maxturn++;
                Invalidate();

            }
        }
        base.onmouseup(e);
    }

    static void Main()
    {
        // NewGame createnewgame = new NewGame();
        Application.Run(new TicTacToe());
    }

}



i kind of understand the setup of minimax loops but ye i dont understand things like
a) am i defining max as 1 and min as -1
b)alpha and beta are basically "posh" words for max and min in the loops?
c) i dont get how to work out a available moves "score"

thanks

This post has been edited by bboyzeez: 18 January 2013 - 11:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Finished "playable" tic tac toe but few problems occuring

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Finished "playable" tic tac toe but few problems occuring

Posted 18 January 2013 - 11:16 AM

I'm going to stop you right here.

Nearly 1000 lines for tic-tac-toe is GROSSLY out of line.
We have numerous students due this as homework every semester and come here with code closer to 100 lines.

These huge blocks of if...else...if...else can easily be converted to loops.

Lines 519 - 526 suffer from the same brute force approach when a loop would have been smaller, dynamic and more elegant.

In short - all of this is a just a hot mess of brute force, copy/paste of code over and over and over.

I strongly urge you to stop rushing through the book just to complete it. You have failed to learn any of the basic ideas in the early chapters for things like how to loop through arrays. Personally, I think you need to go back a couple chapters and pick up the concepts you glossed over so you can start working smarter instead of harder.
Was This Post Helpful? 0
  • +
  • -

#3 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: Finished "playable" tic tac toe but few problems occuring

Posted 18 January 2013 - 11:46 AM

ok that is fine i will go over the code and make it "smarter" once i have done that ill be back for original problems :)
Was This Post Helpful? 0
  • +
  • -

#4 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: Finished "playable" tic tac toe but few problems occuring

Posted 18 January 2013 - 12:06 PM

just before i start if you look at lines 700- 742

how could i make this into a loop if they all draw to different places?
Was This Post Helpful? 0
  • +
  • -

#5 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: Finished "playable" tic tac toe but few problems occuring

Posted 18 January 2013 - 12:36 PM

in reply to "Lines 519 - 526 suffer from the same brute force approach when a loop would have been smaller, dynamic and more elegant."

i can use an array for the win but due to the difference in numbers being different i end up with this,

int a=0 ,b=1 ,c=2;

        for (int i = 0; i <=3; i++)
        {

            win[i] = wongame[a] + wongame[b] + wongame[c];
            a = a + 3;
            b = b + 3;
            c = c + 3;
        }

        for (int i = 3; i <= 6; i++)
        {
            win[i] = wongame[a] + wongame[b +2] + wongame[c+4];
            a = a + 1;
            b = b + 1;
            c = c + 1;
        }

        for (int i = 6; i <= 7; i++)
        {
            win[i] = wongame[a] + wongame[b * 4] + wongame[c + 6];
            a = a + 2;
            c = c - 2;
        }



this isnt smaller but would you say more dynamic and possibly more elegant?

p.s i start university in September so i expect to be given loads of examples to help make things clear that i dont understand but for now i have this book , examples of other programmers online (but most being critised for "bad teaching" and advice from this site
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Finished "playable" tic tac toe but few problems occuring

Posted 18 January 2013 - 12:38 PM

You've hard-coded all your math. That's a bad way to go. There is no scaling possible for example. If you calculate your positions and sizes on the fly you can have the board adjust itself to the size of the form its on.

So the first vertical line is 33% of the total width, from the left edge.
The second vertical line is 66% of the total width, from the left edge.

Calculating the positions for where to draw the x and O would be worked out mathmatically as well. I'd probably make a method that just takes the box number and the symbol to be drawn.

   1 | 2 | 3
   ---------
   4 | 5 | 6
   ---------
   7 | 8 | 9


Now as your loop goes through the boxes it can just call this method with the index+1 of the box that is occupied.



void DrawSymbol(int boxNumber, char XorO)
{
    // calculate the x,y of the box number
    // draw the x or o
}

Was This Post Helpful? 1
  • +
  • -

#7 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: Finished "playable" tic tac toe but few problems occuring

Posted 18 January 2013 - 03:18 PM

ok had a little clean up 870 lines down to 479, i know i waste a lot of lines when it comes to looking for best move , maybe using the minimax theory would dramatically shorten this amount of lines?

anyways here is the game working exactly like before with the same problems i originally asked if anyone could take a look and see how i can get around my problems thanks


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

public class TicTacToe : Form
{
    int maxturn;
    int[] squareempty = new int[9];
    int index = 10;
    int endofgame = 0;
    int whosturn; // 0 for comp 1 for user
    int[] wongame = new int[9];
    GraphicsPath reset = new GraphicsPath();//resetbutton
    Region resetregion = new Region();
    GraphicsPath[] grid = new GraphicsPath[9];// array rectangles
    Region[] gridregion = new Region[9];
    int[] XOvalues = new int[9];
    int[] checkedboxes = new int[9];
    Random Turn = new Random();
    Random r = new Random();
    bool[] check = new bool[24];
    int count;

    bool firstturn = true;
    int[] win = new int[8];
    PointF[] winLine = new PointF[9];
    PointF[] XorOpoints = new PointF[9];
    public TicTacToe()
    {
        Size = new Size(800, 800);
        Text = "Tic Tac Toe";
        BackColor = Color.Red;
        int w = DisplayRectangle.Width - 20;
        int h = DisplayRectangle.Height - 20;
        whosturn = Turn.Next(2);
        if (whosturn == 1)
        { firstturn = false; }
        Point resetpoint = new Point(w / 10, h / 10 * 9);// reset button
        reset.AddString("Reset", FontFamily.GenericSerif, 0, 60, resetpoint, StringFormat.GenericDefault);
        resetregion = new Region(reset);

    }

    public void Initboard(Graphics g)
    {
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;
        int sizeofsquare = w / 3;
        RectangleF[] rectanglepoints = { new RectangleF(w / 20+5, h / 20+5, w / 20 * 6 - 10, h/20 * 5-10), new RectangleF(w / 20 * 7, h / 20+5, w / 20 * 6 - 5, h / 20 * 5-10), new RectangleF(w / 20*13, h / 20+5, w / 20 * 6-5, h / 20 * 5-10), 
                                         new RectangleF(w / 20+5, h / 20*6, w / 20 * 6 - 10, h/20*5-5), new RectangleF(w / 20 * 7, h / 20*6, w / 20 * 6 - 5, h / 20 * 5-5), new RectangleF(w / 20 * 13, h / 20*6, w / 20 * 6-5, h / 20 * 5-5), 
                                       new RectangleF(w / 20+5, h / 20*11, w / 20 * 6 - 10, h/20*5-5), new RectangleF(w / 20 * 7, h/20*11, w / 20 * 6 - 5, h / 20 * 5-5), new RectangleF(w / 20 * 13, h/20*11, w / 20 * 6-5, h / 20 * 5-5)
                                       };
        for (int i = 0; i < rectanglepoints.Length; i++)
        {
            grid[i] = new GraphicsPath();
            grid[i].AddRectangle(rectanglepoints[i]);
            gridregion[i] = new Region(grid[i]);
        }
        Pen outline = new Pen(Color.White, 5);
        g.FillRegion(Brushes.White, resetregion); //reset button
        g.DrawRectangle(outline, w / 10, h / 10 * 9 - 20, w / 10 * 2, h / 10 + 10);
        g.DrawRectangle(Pens.Black, w / 10, h / 10 * 9 - 20, w / 10 * 2, h / 10 + 10);
        g.DrawRectangle(Pens.Black, w / 10 - 3, h / 10 * 9 - 23, w / 10 * 2 + 5, h / 10 + 15);
        g.DrawRectangle(Pens.Black, w / 10 + 3, h / 10 * 9 - 17, w / 10 * 2 - 5, h / 10 + 5);
      

        
        g.FillRectangle(Brushes.White, w / 20, h / 20, w / 20 * 18, h / 20 * 15);//white background for game
       
        Pen Blackline = new Pen(Brushes.Black, 5);
        float t1 = 1  ,t3 = 3;
        for (int i = 0; i <= 3; i++)                        // draws vertical grid lines
        {
            g.DrawLine(Blackline,(w / 20) - 1, (h / 20) * t1 - 3, (w / 20) * 19, (h / 20) * t1 -t3);
            t1 = t1 + 5;
            
        }

        t1 = 1; t3 = 3;
        for (int i = 0; i <= 3; i++)                        // draws horizontal grid lines
        {
            g.DrawLine(Blackline, (w / 20) * t1 - t3, (h / 20), (w / 20) * t1 - t3, h / 20 * 16 - 3);
            t1 = t1 + 6;
        }
      
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        Initboard(g);
        CheckForWin(g);
        DrawXorO(g);
        AiDefendorWinMove();
        AiBestMove();
        base.OnPaint(e);
    }


    public void AiDefendorWinMove()
    {
        if (endofgame == 0 && whosturn == 0)
        {
            if (whosturn == 0 && firstturn == true)
            {
                int firstgo = r.Next(9);
                index = firstgo;
                XOvalues[index] = 5;
                checkedboxes[index] = index;
                wongame[index] = 5;
                whosturn = 1;
                firstturn = false;
                maxturn++;
                Invalidate();

            }
            //check to stop player winning , loops below checks first two columns 
            int j = 0, k = 1, l = 2; count = 0;
            for (int i = 0; i <= 2; i++)
            {
                if (XOvalues[j] == XOvalues[k] && XOvalues[l] == 0 && check[count] == false && whosturn == 0 && XOvalues[j] != 0)
                {
                    index = l;
                    XOvalues[index] = 5;
                    checkedboxes[index] = index;
                    wongame[index] = 5;
                    whosturn = 1;
                    check[i] = true;
                    maxturn++;
                    Invalidate();
                }
                j = j + 3;
                k = k + 3;
                l = l + 3;
                count++;
            }

            //check to stop player winning , loops below checks last two columns  
            j = 1; k = 2; l = 0; 
            for (int i = 0; i <= 2; i++)
            {
                if (XOvalues[j] == XOvalues[k] && XOvalues[l] == 0 && check[count] == false && whosturn == 0 && XOvalues[j] != 0)
                {
                    index = l;
                    XOvalues[index] = 5;
                    checkedboxes[index] = index;
                    wongame[index] = 5;
                    whosturn = 1;
                    check[count] = true;
                    maxturn++;
                    Invalidate();
                }
                j = j + 3;
                k = k + 3;
                l = l + 3;
                count++;
            }

            //check to stop player winning , loops below checks first and last columns  
            j = 0; k = 2; l = 1;
            for (int i = 0; i <= 2; i++)
            {
                if (XOvalues[j] == XOvalues[k] && XOvalues[l] == 0 && check[count] == false && whosturn == 0 && XOvalues[j] != 0)
                {
                    index = l;
                    XOvalues[index] = 5;
                    checkedboxes[index] = index;
                    wongame[index] = 5;
                    whosturn = 1;
                    check[count] = true;
                    maxturn++;
                    Invalidate();
                }
                j = j + 3;
                k = k + 3;
                l = l + 3;
                count++;
            }
            //check to stop player winning , loops below checks first 2 rows 
            j = 0; k = 3; l = 6;
            for (int i = 0; i <= 2; i++)
            {
                if (XOvalues[j] == XOvalues[k] && XOvalues[l] == 0 && check[count] == false && whosturn == 0 && XOvalues[j] != 0)
                {
                    index = l;
                    XOvalues[index] = 5;
                    checkedboxes[index] = index;
                    wongame[index] = 5;
                    whosturn = 1;
                    check[count] = true;
                    maxturn++;
                    Invalidate();
                }
                j = j + 1;
                k = k + 1;
                l = l + 1;
                count++;
            }

            //check to stop player winning , loops below checks last 2 rows 
            j = 3; k = 6; l = 0;
            for (int i = 0; i <= 2; i++)
            {
                if (XOvalues[j] == XOvalues[k] && XOvalues[l] == 0 && check[count] == false && whosturn == 0 && XOvalues[j] != 0)
                {
                    index = l;
                    XOvalues[index] = 5;
                    checkedboxes[index] = index;
                    wongame[index] = 5;
                    whosturn = 1;
                    check[count] = true;
                    maxturn++;
                    Invalidate();
                }
                j = j + 1;
                k = k + 1;
                l = l + 1;
                count++;
            }

            //check to stop player winning , loops below checks first and last rows
            j = 0; k = 6; l = 3;
            for (int i = 0; i <= 2; i++)
            {
                if (XOvalues[j] == XOvalues[k] && XOvalues[l] == 0 && check[count] == false && whosturn == 0 && XOvalues[j] != 0)
                {
                    index = l;
                    XOvalues[index] = 5;
                    checkedboxes[index] = index;
                    wongame[index] = 5;
                    whosturn = 1;
                    check[count] = true;
                    maxturn++;
                    Invalidate();
                }
                j = j + 1;
                k = k + 1;
                l = l + 1;
                count++;
            }

           //check to stop player winning , loops below checks diagonal wins
            j = 0; k = 4; l = 8;
            for (int i = 0; i <= 1; i++)
            {
                if (XOvalues[j] == XOvalues[k] && XOvalues[l] == 0 && check[count] == false && whosturn == 0 && XOvalues[j] != 0)
                {
                    index = l;
                    XOvalues[index] = 5;
                    checkedboxes[index] = index;
                    wongame[index] = 5;
                    whosturn = 1;
                    check[count] = true;
                    maxturn++;
                    Invalidate();
                }
                j = j + 2;
                l = l - 2;
                count++;
            }

            j = 6; k = 4; l = 2;
            for (int i = 0; i <=1; i++)
            {
                if (XOvalues[j] == XOvalues[k] && XOvalues[l] == 0 && check[count] == false && whosturn == 0 && XOvalues[j] != 0)
                {
                    index = l;
                    XOvalues[index] = 5;
                    checkedboxes[index] = index;
                    wongame[index] = 5;
                    whosturn = 1;
                    check[count] = true;
                    maxturn++;
                    Invalidate();
                }
                j = j + 2;
                l = l - 2;
                count++;
            }

            j = 0; k = 8; l = 4;
            for (int i = 0; i <= 1; i++)
            {
                if (XOvalues[j] == XOvalues[k] && XOvalues[l] == 0 && check[count] == false && whosturn == 0 && XOvalues[j] != 0)
                {
                    index = l;
                    XOvalues[index] = 5;
                    checkedboxes[index] = index;
                    wongame[index] = 5;
                    whosturn = 1;
                    check[count] = true;
                    maxturn++;
                    Invalidate();
                }
                j = j + 2;
                k = k - 2;
                count++;
            }
         }
    }
    public void AiBestMove()
    {
        if (endofgame == 0)
        {

            if (whosturn == 0)
            {
                int Bestmove = r.Next(9);

                for (int i = 0; i < 100; i++)
                {
                    if (XOvalues[Bestmove] == 0)
                    {
                        index = Bestmove;
                        XOvalues[index] = 5;
                        checkedboxes[index] = index;
                        wongame[index] = 5;
                        whosturn = 1;
                        maxturn++;
                        Invalidate();
                        break;
                    }
                    else
                    {
                        Bestmove = r.Next(9);
                    }
                }
            }

        }
    }
    public void CheckForWin(Graphics g)
    {
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;
        String Userwins = "User Wins!";
        String CompWins = "Computer Wins!";
        
        Font winfont = new Font("Arial", w / 20, FontStyle.Regular);
        Font wincompfont = new Font("Arial", 30, FontStyle.Regular);

        // Total all possible ways of winning
        int a=0 ,b=1 ,c=2;

        for (int i = 0; i <=2; i++)
        {

            win[i] = wongame[a] + wongame[b] + wongame[c];
            a = a + 3;
            b = b + 3;
            c = c + 3;
        }
        a = 0; b = 3; c = 6;
        for (int i = 3; i <= 5; i++)
        {
            win[i] = wongame[a] + wongame[b] + wongame[c];
            a = a + 1;
            b = b + 1;
            c = c + 1;
        }
         a = 0; b = 4; c = 8;
        for (int i = 6; i <= 7; i++)
        {
            win[i] = wongame[a] + wongame[b ] + wongame[c ];
            a = 2;
            b = 4;
            c = 6;
        }

        //Check to see if anyone has won

        for (int i = 0; i <=7; i++)
        {
            if (win[i] == 6)
            {
                g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17);
                endofgame = 1;
            }
            if (win[i] == 15)
            {
                g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10);
                endofgame = 1;
            }
        }
       
         // see if there is draw

        if (maxturn == 9)
        {
            if (endofgame == 0)
            {
                g.DrawString("Draw!", winfont, Brushes.Black, w / 20 * 10, h / 20 * 17);
            }

        }
    }
    
    public void DrawXorO(Graphics g)
    {
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;
        Font arial = new Font("Arial", w/20*3, FontStyle.Regular);
        PointF[] XorOpoints = { new PointF(w / 20 + 10, h / 20 - 10), new PointF(w / 20 * 7 + 15, h / 20 - 10) , new PointF(w/20*13 + 15, h / 20 - 10),
                              new PointF(w / 20 + 10, h / 20 * 5 + 20), new PointF(w / 20 * 7 + 15, h / 20 * 5 + 20) , new PointF(w/20*13 + 15, h / 20 * 5 + 20),
                              new PointF(w / 20 + 10, h / 20 *10+20), new PointF(w / 20 * 7 + 15, h / 20 *10+20) , new PointF(w/20*13 + 15,h / 20 *10+20)};
        for (int x = 0; x <= 8; x++)
        {
            if (checkedboxes[x] == x && XOvalues[x] == 2)
            {
                g.DrawString("X", arial, Brushes.Black, XorOpoints[x].X, XorOpoints[x].Y);
            }
         }
        for (int x = 0; x <= 8; x++)
        {
            if (checkedboxes[x] == x && XOvalues[x] == 5)
            {
                g.DrawString("O", arial, Brushes.Black, XorOpoints[x].X, XorOpoints[x].Y);
            }
        }
  }
    protected override void onmousedown(MouseEventArgs e)
    {
        RectangleF resetrect = resetregion.GetBounds(CreateGraphics());// reset button
        if (resetrect.Contains(e.X, e.Y))
        {
           Application.Restart();
        }
        Boolean found = false;
        int i = 0;
        while (!found && i < gridregion.Length && endofgame == 0)// click in squares
        {
            RectangleF[] scans = gridregion[i].GetRegionScans(new Matrix());
            int j = 0;
            while (!found && j < scans.Length)
            {
                if (scans[j].Contains(e.X, e.Y) && XOvalues[i] == 0)
                {

                    index = i;

                    found = true;


                }
                j++;
            }
            i++;
        }
        base.onmousedown(e);
    }

    protected override void onmouseup(MouseEventArgs e)
    {
        if (endofgame == 0)
        {
            if (index < 10 && whosturn == 1 && XOvalues[index] == 0)
            {
                XOvalues[index] = 2;
                checkedboxes[index] = index;
                wongame[index] = 2;
                whosturn = 0;
                maxturn++;
                Invalidate();

            }
        }
        base.onmouseup(e);
    }

    static void Main()
    {
        Application.Run(new TicTacToe());
    }

}


This post has been edited by bboyzeez: 18 January 2013 - 03:19 PM

Was This Post Helpful? 1
  • +
  • -

#8 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Finished "playable" tic tac toe but few problems occuring

Posted 18 January 2013 - 04:57 PM

OK, I'm a novice myself, and maybe someone that really knows what they're talking about can correct me, but...

Your original problem was you couldn't figure out how to prioritize going for a win over going to block a win. That should be really simple for OO code, but your code is in many ways not broken down very well. What you really need to do is break everything up into more discrete chunks. The structure would be something like:

private void nextMove
 {
   int canIWin = checkWin(O) //checks to see if the 
                             //computer can win in the next move
   if (canIWin >0)
   {
      //play in the canIWin square.
   }

   int canHeWin = checkWin(X) //now check to see if opponent can win.
   if (canIWin ==0 && canHeWin >0)
   {
      //play in teh canHeWin squre
   }

   if (canHeWin == 0 && canIWin == 0)
   {
      //Logic for figuring out how to play next when no one can win next move.
   }

   private int checkWin (string player)
   {
      //Initialize the return int variable as a 0 and then Check every
      //square if it would win the game for that player. If it does, 
      //set the return int to that square number.
   }
}


This post has been edited by pharylon: 18 January 2013 - 05:10 PM

Was This Post Helpful? 1
  • +
  • -

#9 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Finished "playable" tic tac toe but few problems occuring

Posted 18 January 2013 - 05:03 PM

My example is just one way to do it and I'm sure there are better ones. But, the point is, if your problem is you need to do X in one case and Y in another, you need to check those two conditions separately and use logic based on that checking.

This post has been edited by pharylon: 18 January 2013 - 05:06 PM

Was This Post Helpful? 1
  • +
  • -

#10 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: Finished "playable" tic tac toe but few problems occuring

Posted 18 January 2013 - 05:24 PM

thank you :) i understand that i will work it out tomorrow now as its late :)
Was This Post Helpful? 0
  • +
  • -

#11 Tanira  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 102
  • Joined: 30-May 09

Re: Finished "playable" tic tac toe but few problems occuring

Posted 20 January 2013 - 08:30 AM

Hello,

I like your idea but here is a suggestion on how to make the checkWin(string player) function a bit more understandable.


private bool checkWin(bool isFirstPlayer, out int square){}

or

private bool checkWin(int numPlayer, out int square){}




If you set the out variable to the square number than the method can return a boolean which is much more natural.
Usually in tic-tac-toe there is only two players so a boolean could work better than a string to determine which player to check.

However if you wanted to make it expandable for more players than an int could work as well.

View Postpharylon, on 18 January 2013 - 04:57 PM, said:

OK, I'm a novice myself, and maybe someone that really knows what they're talking about can correct me, but...

Your original problem was you couldn't figure out how to prioritize going for a win over going to block a win. That should be really simple for OO code, but your code is in many ways not broken down very well. What you really need to do is break everything up into more discrete chunks. The structure would be something like:

private void nextMove
 {
   int canIWin = checkWin(O) //checks to see if the 
                             //computer can win in the next move
   if (canIWin >0)
   {
      //play in the canIWin square.
   }

   int canHeWin = checkWin(X) //now check to see if opponent can win.
   if (canIWin ==0 && canHeWin >0)
   {
      //play in teh canHeWin squre
   }

   if (canHeWin == 0 && canIWin == 0)
   {
      //Logic for figuring out how to play next when no one can win next move.
   }

   private int checkWin (string player)
   {
      //Initialize the return int variable as a 0 and then Check every
      //square if it would win the game for that player. If it does, 
      //set the return int to that square number.
   }
}


Was This Post Helpful? 0
  • +
  • -

#12 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: Finished "playable" tic tac toe but few problems occuring

Posted 21 January 2013 - 04:05 PM

Hi bboyzeez ,

my advice to you is to spend some time learning design patterns, and refactoring techniques. learn about code reusability, and SOC, polymorphism, encapsulation and abstraction. once you have a good graps on these concepts then you can apply to your code

good luck
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1