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

New Topic/Question
Reply



MultiQuote



|