9 Replies - 6513 Views - Last Post: 16 January 2013 - 01:26 PM Rate Topic: -----

#1 bboyzeez  Icon User is offline

  • D.I.C Head

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

problem with Nim Game , getregionscans

Posted 14 January 2013 - 01:54 PM

hi all , so i have to make a game called Nim , your allowed to take three at a time then press n when its your turn over and then the computer takes its turn. when the computer takes its turns it works fine but when the user gets to token 6(going from the left) it looks to be saying its token 5 instead and not working? im guessing its a problem with the getregionscans but not quite sure could anyone take a look and run it and see what im doing wrong please?

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 Nim : Form
{
    int go = 0;
    Random r = new Random();
    Random userr = new Random();
    int userchoice;
    int turn; 
    Point[] verts = new Point[15];
    Rectangle[] rect = new Rectangle[15];
    GraphicsPath [] token = new GraphicsPath[15];
    Region[] region = new Region[15];
    Font arial = new Font("Arial", 20, FontStyle.Bold);
    private float [] oldX = new float[15];
    private float [] oldY = new float[15];
    private int index;
    int count;
    bool endturn = false;
    public Nim()
    {
        Size = new Size(800, 800);
        Text = "Nim";
        BackColor = Color.Green;
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;
        int startturn =  r.Next(1, 3);
        turn = startturn;
        
        for (int i = 0; i < 15; i++)
        {
            verts[i] = new Point(w / 15 * i + 10, h / 10 * 5);
            rect[i] = new Rectangle(verts[i].X, verts[i].Y, 30, 30);
            token[i] = new GraphicsPath();
            token[i].AddEllipse(rect[i]);
            //g.FillEllipse(Brushes.WhiteSmoke, rect[i]);
            region[i] = new Region(token[i]);
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;

        for (int i = 0; i < region.Length; i++)
        {
            g.FillRegion(Brushes.WhiteSmoke, region[i]);
        }
        Pen blackline = new Pen(Brushes.Black, 15);
        g.DrawLine(blackline, 0, h / 10 * 5 - 20, w / 10 * 10, h / 10 * 5 - 20);
        g.DrawLine(blackline, 0, h / 10 * 5 + 50, w / 10 * 10, h / 10 * 5 + 50);
        //user side
        int wordlength = (int)g.MeasureString("User Tokens", arial).Width;
        int userstart = (Width - wordlength) / 2;
        g.DrawString("Users Tokens", arial, Brushes.WhiteSmoke, userstart, h / 10 * 8);
        g.DrawLine(blackline, 0, h / 10 * 8 + 50, w / 10 * 10, h / 10 * 8 + 50);
        //comp side
        int complength = (int)g.MeasureString("Computer's Tokens", arial).Width;
        int compstart = (Width - complength) / 2;
        g.DrawString("Computer's Tokens", arial, Brushes.WhiteSmoke, compstart, h / 10 * 2 + 10);

        g.DrawLine(blackline, 0, h / 10 * 2, w / 10 * 10, h / 10 * 2);

        



        /////////////////////////////////////////////////////////////////////////////
        //              Order of Turn                                       ////////
        ///////////////////////////////////////////////////////////////////////////
        if (count < 15)
        {

            if (turn == 1)
            {
                userchoice = userr.Next(1, 4);

                for (int x = 0; x < userchoice+1; x++)
                {
                    if (x == 1)
                    { x = 0; }

                    region[count ].Translate(0, -h / 10 * 4);
                    if (x == 0)
                    { x = 1; }
                    count++;
                    Invalidate();

                }
                    turn = 2;
                g.DrawString("Computers Turn", arial, Brushes.Green, w / 10 * 3, h / 10 * 3);
            }
                 base.OnPaint(e);
        }
    }

    // user turn

    protected override void onmousedown(MouseEventArgs e)
    {
        bool found = false;
        int i = 0;
        while (!found && i < region.Length)
        {
            RectangleF[] scans = region[i].GetRegionScans(new Matrix());
            int j = 0;
            while (!found && j < scans.Length)
            {
                if (scans[j].Contains(e.X, e.Y))
                {
                    oldX[i] = e.X;
                    oldY[i] = e.Y;
                    index = i;
                    found = true;
                }
                j++;
            }
            i++;
        }
        
        base.onmousedown(e);
    }
    protected override void onmouseup(MouseEventArgs e)
    {
        

        if (go < 3)
        {


            int w = DisplayRectangle.Width;
            int h = DisplayRectangle.Height;
            region[index].Translate(0, h / 10 * 4);
            count++;
            go++;
        }
        Invalidate();
        base.onmouseup(e);
    }
    //start Game
    protected override void onkeydown(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.N)
        {
            turn = 1;
            Invalidate();
        }
        base.onkeydown(e);
    }

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







thank you

Is This A Good Question/Topic? 0
  • +

Replies To: problem with Nim Game , getregionscans

#2 bboyzeez  Icon User is offline

  • D.I.C Head

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

Re: problem with Nim Game , getregionscans

Posted 14 January 2013 - 02:32 PM

oh just realised i left in the code about oldx and oldy that was from the previosu challenge i got rid of them now incase anyone asks why i have included them lines
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: problem with Nim Game , getregionscans

Posted 14 January 2013 - 02:38 PM

maybe if someone can help me understand why when it creates a rectangle array for each region why does it create 17 arrays? surely it should just be a rectangle coords where each ellipse is?
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: problem with Nim Game , getregionscans

Posted 14 January 2013 - 03:47 PM

ok after trying at that attempt i thought its alot easier(for my understanding) to just make the tokens dissapear after you choose an amount between 1-3 so i got this to work just one question , is there anyway to allow the program to sit on the final results screen before it exits?


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 Nim : Form
{
    int go = 0;
    Random r = new Random();
    Random userr = new Random();
    int userchoice;
    int turn; 
    Point[] verts = new Point[15];
    Rectangle[] rect = new Rectangle[15];
    GraphicsPath [] token = new GraphicsPath[15];
    GraphicsPath one = new GraphicsPath();
    GraphicsPath two = new GraphicsPath();
    GraphicsPath three = new GraphicsPath();
    Region[] region = new Region[15];
    Font arial = new Font("Arial", 20, FontStyle.Bold);
    private float [] oldX = new float[15];
    private float [] oldY = new float[15];
    private int index;
    int count=14;
    bool endturn = false;
    bool inside = false;
    Region oneregion = new Region();
    Region tworegion = new Region();
    Region threeregion = new Region();
    int choiceT;
    public Nim()
    {
        Size = new Size(800, 800);
        Text = "Nim";
        BackColor = Color.Green;
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;
        int startturn =  r.Next(1, 3);
        turn = startturn;
        //create 1 option
        Point onepoint = new Point(w / 10 * 1, h / 10 * 6);
        one.AddString("1", FontFamily.GenericSansSerif, 1, 100, onepoint, StringFormat.GenericDefault);
        oneregion = new Region(one);

        //create 2 option
        Point twopoint = new Point(w / 10 * 5-40, h / 10 * 6);
        two.AddString("2", FontFamily.GenericSansSerif, 1, 100, twopoint, StringFormat.GenericDefault);
        tworegion = new Region(two);

        //create 3 option
        Point threepoint = new Point(w / 10 * 8, h / 10 * 6);
        three.AddString("3", FontFamily.GenericSansSerif, 1, 100, threepoint, StringFormat.GenericDefault);
        threeregion = new Region(three);

        for (int i = 0; i < 15; i++)
        {
            verts[i] = new Point(w / 15 * i + 10, h / 10 * 5);
            rect[i] = new Rectangle(verts[i].X, verts[i].Y, 30, 30);
            token[i] = new GraphicsPath();
            token[i].AddEllipse(rect[i]);
            //g.FillEllipse(Brushes.WhiteSmoke, rect[i]);
            region[i] = new Region(token[i]);
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;

        g.FillRegion(Brushes.White, oneregion);
        g.FillRegion(Brushes.White, tworegion);
        g.FillRegion(Brushes.White, threeregion);

        for (int i = 0; i < region.Length; i++)
        {
            g.FillRegion(Brushes.WhiteSmoke, region[i]);
        }
        Pen blackline = new Pen(Brushes.Black, 15);
        g.DrawLine(blackline, 0, h / 10 * 5 - 20, w / 10 * 10, h / 10 * 5 - 20);
        g.DrawLine(blackline, 0, h / 10 * 5 + 50, w / 10 * 10, h / 10 * 5 + 50);
         g.DrawLine(blackline, 0, h / 10 * 8 + 50, w / 10 * 10, h / 10 * 8 + 50);
        
        g.DrawLine(blackline, 0, h / 10 * 2, w / 10 * 10, h / 10 * 2);


        if (count <= 0 && turn == 1)
        {
            g.DrawString("User Wins", arial, Brushes.White, w / 10 * 4, h / 10 * 2);
            
            Application.Exit();
        }

        else if (count <= 0 && turn == 2)
        {
            g.DrawString("Computer Wins", arial, Brushes.White, w / 10 * 4, h / 10 * 2);
            Application.Exit();
        }


        /////////////////////////////////////////////////////////////////////////////
        //              Order of Turn                                       ////////
        ///////////////////////////////////////////////////////////////////////////
        if (count != 1)
        {
            
            if (turn == 1)
            {
                userchoice = userr.Next(1, 4);

                for (int x = 0; x < userchoice+1; x++)
                {
                    if (count == 0)
                    {
                        g.DrawString("User Wins", arial, Brushes.White, w / 10 * 4, h / 10 * 2);
                        break;
                    }

                    else
                    {
                        

                        region[count].MakeEmpty();
                        if (x == 0)
                        { x = 1; }
                        count--;
                    }
                    Invalidate();

                }
                    turn = 2;
            }
                 base.OnPaint(e);
        }
    }

    // user turn

    protected override void onmousedown(MouseEventArgs e)
    {
        if (oneregion.GetBounds(CreateGraphics()).Contains(e.X,e.Y))
        {
             choiceT = 1;
           
        }

        if (tworegion.GetBounds(CreateGraphics()).Contains(e.X,e.Y))
        {
            choiceT = 2;
        }

        if (threeregion.GetBounds(CreateGraphics()).Contains(e.X,e.Y))
        {
             choiceT = 3;
        }
        
        base.onmousedown(e);
    }
    protected override void onmouseup(MouseEventArgs e)
    {
        
             int w = DisplayRectangle.Width;
            int h = DisplayRectangle.Height;

            for (int x = 0; x < choiceT; x++)
            {
                if (count == 0)
                {
                    
                    break;
                }
                region[count].MakeEmpty();
                count--;
                go++;
            }
            choiceT = 0;
            turn = 1;
        Invalidate();
        base.onmouseup(e);
    }
    //start Game
    protected override void onkeydown(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Space)
        {
            
            Invalidate();
        }

        if (e.KeyCode == Keys.N)
        {
            turn = 1;
            Invalidate();
        }
        base.onkeydown(e);
    }

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

public class Compsturn : Nim
{
    public void Usergo(Graphics g)
    {   Font arial = new Font("Arial", 20, FontStyle.Bold);
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;
        g.DrawString("Computers Turn", arial, Brushes.WhiteSmoke, w / 10 * 3, h / 10 * 3);
    }
}

public class Userturn : Nim
{
    public void UserGo(Graphics g)
    {
        Font arial = new Font("Arial", 20, FontStyle.Bold);
        int w = DisplayRectangle.Width;
        int h = DisplayRectangle.Height;
        g.DrawString("Players Turn", arial, Brushes.WhiteSmoke, w / 10 * 4, h / 10 * 6);

        
       
    }
}




Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: problem with Nim Game , getregionscans

Posted 14 January 2013 - 03:51 PM

Get rid of the Application.Exit() calls.

Actually, the existence of those Application.Exit() calls is a pretty good indication that code was ported from C, because most self respecting C# programmers using WinForms would only call Application.Exit() from within in a form when it is a dire emergency.
Was This Post Helpful? 0
  • +
  • -

#6 bboyzeez  Icon User is offline

  • D.I.C Head

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

Re: problem with Nim Game , getregionscans

Posted 14 January 2013 - 04:49 PM

but if i get rid of them it will just continue to loop the app?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: problem with Nim Game , getregionscans

Posted 14 January 2013 - 05:44 PM

Yes. Welcome to the world of event driven programming. Time to dump your console programming ways.
Was This Post Helpful? 0
  • +
  • -

#8 bboyzeez  Icon User is offline

  • D.I.C Head

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

Re: problem with Nim Game , getregionscans

Posted 14 January 2013 - 08:02 PM

So I should use something like this.close or environment.exit()?
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: problem with Nim Game , getregionscans

Posted 15 January 2013 - 05:26 AM

Yes, you would call the Close() method of your Form, and do it from an event handler that corresponds to a button or menu item.

Your current code structure of doing game logic from within the paint event handler isn't the best way to architect a program. Remember that the WM_PAINT message is a low priority message in Windows. So keyboard and mouse inputs can override paint messages. If your code is dependent on some game logic being done as a result of your Invalidate() call from within your click handlers, you are living on borrowed time before you start seeing "weird" bugs.
Was This Post Helpful? 0
  • +
  • -

#10 bboyzeez  Icon User is offline

  • D.I.C Head

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

Re: problem with Nim Game , getregionscans

Posted 16 January 2013 - 01:26 PM

thanks skydiver, yes i had a quick "scan" through the book i am learning from and i see how it gets more complex, i know i have a lot to learn :) when i joined the site a month ago today ( :) ) i had nothing so i am happy with being at this stage within a month of learning and handling a full time job :)and i know i will hopefully learn at faster rate once i get into university in September:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1