shah123

How to record a move of player I am assuming in AddUser() if username entered is pc then PC image will be added and will start playing with human

I have following piece of code: Please help

Code Snippet

int movenum;

private int MoveNumber
{
get
{
return movenum;
}
set
{
movenum = value;
}
}


public void AddUser(int user, string strText, string img)
{
lbls = new Label[] { label2, label4 };
pics = new PictureBox[] { pictureBox1, pictureBox2 };

user--;
if (strText.Equals(""))
{
lbls[user].Text = "Guest";
}
else
{
lbls[user].Text = strText;
}
if (strText.Equals("pc"))
{
pics[user].SizeMode = PictureBoxSizeMode.StretchImage;
pics[user].Load("C:\\WINDOWS\\system32\\setup.bmp");
}
else
{
pics[user].SizeMode = PictureBoxSizeMode.StretchImage;
pics[user].Load(img);
}

userLoggedIn++;
}
private void switchPlayers()
{
if (player == 1)
{
player = 2;
}
else
{
player = 1;
}

checkAll();
}

private void box1_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box1.SizeMode = PictureBoxSizeMode.StretchImage;
box1.Image = pics[player - 1].Image;
box1.Enabled = false;
switchPlayers();
}

}

private void box2_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box2.SizeMode = PictureBoxSizeMode.StretchImage;
box2.Image = pics[player-1].Image;
box2.Enabled = false;
switchPlayers();
}

}

private void box3_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box3.SizeMode = PictureBoxSizeMode.StretchImage;
box3.Image = pics[player - 1].Image;
box3.Enabled = false;
switchPlayers();
}

}
public bool CompareImages(Image image1,Image image2,Image image3)
{
if(!Object.Equals(image1,image2))
{
return false;
}
if(!Object.Equals(image2,image3))
{
return false;
}
if ((image1 == null) || (image2 == null) || (image3 == null))
{
return false;
}
return true;

}

public void checkAll()
{
if (CompareImages(box1.Image, box2.Image, box3.Image))
{
DisplayWinner();

}
else if (CompareImages(box4.Image, box5.Image, box6.Image))
{
DisplayWinner();

}
else if (CompareImages(box7.Image, box8.Image, box9.Image))
{
DisplayWinner();

}
else if (CompareImages(box1.Image, box5.Image, box9.Image))
{
DisplayWinner();

}
else if (CompareImages(box3.Image, box5.Image, box7.Image))
{
DisplayWinner();

}
else if (CompareImages(box1.Image, box4.Image, box7.Image))
{
DisplayWinner();

}
else if (CompareImages(box2.Image, box5.Image, box8.Image))
{
DisplayWinner();

}
else if (CompareImages(box3.Image, box6.Image, box9.Image))
{
DisplayWinner();

}
else if (box1.Image != null && box2.Image != null && box3.Image != null && box4.Image != null && box5.Image != null
&& box6.Image != null && box7.Image != null && box8.Image != null && box9.Image != null)
{
lblDraw.Visible = true;
lblDraw.Enabled = true;
//reset();
}

}




Re: Visual C# General How to record the player move

Figo Fei - MSFT

Hi, shah

Which part you feel difficult Logic part or algorithmic part or implementing part

Let us narrow the question scope, shall we

Thanks






Re: Visual C# General How to record the player move

shah123

I need logic or any algoritm will be very helpful. Below is my code

Next action is to make Computer to make its move..Best possible move against action of player.

Code Snippet

public partial class TicTacToe : Form
{
public int player = 1;
private PictureBox[] pics;
private Label[] lbls;
private int userLoggedIn = 0;
public int computer;
private List<int>[] moves = new List<int>[2] { new List<int>(), new List<int>() };


public TicTacToe()
{
InitializeComponent();

}

int movenum=1;

private int MoveNumber
{
get
{
return movenum;
}
set
{
movenum = value;
}
}

public void ComputerMove()
{

}

public void AddUser(int user, string strText, string img)
{
lbls = new Label[] { label2, label4 };
pics = new PictureBox[] { pictureBox1, pictureBox2 };

user--;
if (strText.Equals(""))
{
lbls[user].Text = "Guest";
}
else
{
lbls[user].Text = strText;
}
if (strText.Equals("pc"))
{
pics[user].SizeMode = PictureBoxSizeMode.StretchImage;
pics[user].Load("C:\\WINDOWS\\system32\\setup.bmp");

}
else
{
pics[user].SizeMode = PictureBoxSizeMode.StretchImage;
pics[user].Load(img);
}

userLoggedIn++;
}


private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void switchPlayers()
{
if (player == 1)
{
player = 2;
}
else
{
player = 1;
}

checkAll();
}

private void box1_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box1.SizeMode = PictureBoxSizeMode.StretchImage;
box1.Image = pics[player - 1].Image;
moves[player - 1].Add(1);
++this.movenum;
box1.Enabled = false;
switchPlayers();
}
MessageBox.Show("Move number :" + movenum);
}

private void box2_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box2.SizeMode = PictureBoxSizeMode.StretchImage;
box2.Image = pics[player-1].Image;
moves[player - 1].Add(2);
++this.movenum;
box2.Enabled = false;
switchPlayers();
}
MessageBox.Show("Move number :" + movenum);
}

private void box3_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box3.SizeMode = PictureBoxSizeMode.StretchImage;
box3.Image = pics[player - 1].Image;
moves[player - 1].Add(3);
++this.movenum;
box3.Enabled = false;
switchPlayers();
}
MessageBox.Show("Move number :" + movenum);
}

private void box4_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box4.SizeMode = PictureBoxSizeMode.StretchImage;
box4.Image = pics[player - 1].Image;
moves[player - 1].Add(4);
++this.movenum;
box4.Enabled = false;
switchPlayers();
}
MessageBox.Show("Move number :" + movenum);
}

private void box5_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box5.SizeMode = PictureBoxSizeMode.StretchImage;
box5.Image = pics[player - 1].Image;
moves[player - 1].Add(5);
++this.movenum;
box5.Enabled = false;
switchPlayers();
}
MessageBox.Show("Move number :" + movenum);
}

private void box6_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box6.SizeMode = PictureBoxSizeMode.StretchImage;
box6.Image = pics[player - 1].Image;
moves[player - 1].Add(6);
++this.movenum;
box6.Enabled = false;
switchPlayers();
}
MessageBox.Show("Move number :" + movenum);
}

private void box7_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box7.SizeMode = PictureBoxSizeMode.StretchImage;
box7.Image = pics[player - 1].Image;
moves[player - 1].Add(7);
++this.movenum;
box7.Enabled = false;
switchPlayers();
}
MessageBox.Show("Move number :" + movenum);
}

private void box8_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box8.SizeMode = PictureBoxSizeMode.StretchImage;
box8.Image = pics[player - 1].Image;
moves[player - 1].Add(8);
++this.movenum;
box8.Enabled = false;
switchPlayers();
}
MessageBox.Show("Move number :" + movenum);
}

private void box9_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box9.SizeMode = PictureBoxSizeMode.StretchImage;
box9.Image = pics[player - 1].Image;
moves[player - 1].Add(9);
++this.movenum;
box9.Enabled = false;
switchPlayers();
}
MessageBox.Show("Move number :" + movenum);
}

private void resetBtn_Click(object sender, EventArgs e)
{
reset();
}

private void startBtn_Click(object sender, EventArgs e)
{
displayAgain();
}

public void reset()
{
box1.Image = null;
box2.Image = null;
box3.Image = null;
box4.Image = null;
box5.Image = null;
box6.Image = null;
box7.Image = null;
box8.Image = null;
box9.Image = null;
lblDraw.Visible = false;
displayAgain();

}

public void displayAgain()
{
box1.Enabled = true;
box2.Enabled = true;
box3.Enabled = true;
box4.Enabled = true;
box5.Enabled = true;
box6.Enabled = true;
box7.Enabled = true;
box8.Enabled = true;
box9.Enabled = true;
lblDraw.Visible = false;
}

public bool CompareImages(Image image1,Image image2,Image image3)
{
if(!Object.Equals(image1,image2))
{
return false;
}
if(!Object.Equals(image2,image3))
{
return false;
}
if ((image1 == null) || (image2 == null) || (image3 == null))
{
return false;
}
return true;

}

public void checkAll()
{
if (CompareImages(box1.Image, box2.Image, box3.Image))
{
DisplayWinner();

}
else if (CompareImages(box4.Image, box5.Image, box6.Image))
{
DisplayWinner();

}
else if (CompareImages(box7.Image, box8.Image, box9.Image))
{
DisplayWinner();

}
else if (CompareImages(box1.Image, box5.Image, box9.Image))
{
DisplayWinner();

}
else if (CompareImages(box3.Image, box5.Image, box7.Image))
{
DisplayWinner();

}
else if (CompareImages(box1.Image, box4.Image, box7.Image))
{
DisplayWinner();

}
else if (CompareImages(box2.Image, box5.Image, box8.Image))
{
DisplayWinner();

}
else if (CompareImages(box3.Image, box6.Image, box9.Image))
{
DisplayWinner();

}
else if (box1.Image != null && box2.Image != null && box3.Image != null && box4.Image != null && box5.Image != null
&& box6.Image != null && box7.Image != null && box8.Image != null && box9.Image != null)
{
lblDraw.Visible = true;
lblDraw.Enabled = true;
//reset();
}

}

public void DisplayWinner()
{
if (player == 2) player = 1;
else player = 2;
MessageBox.Show("Player" + player + "wins" ,"Winner");
reset();
displayAgain();
}


public void checkComputerMoves()
{

}
}

}


Thanks





Re: Visual C# General How to record the player move

Thomas Danecker

What you're looking for is the command design pattern (http://en.wikipedia.org/wiki/Command_pattern). Google for it and you will find a lot of hints.




Re: Visual C# General How to record the player move

shah123

Thanks Thomas..but i didnt get it





Re: Visual C# General How to record the player move

Thomas Danecker

If I understand you right, you want to record every move of a player, so you have to encapsulate the operation in an object which you can store (e.g. in a list). That's what the command pattern is about. After you have encapsulated the operation in an object, you can do withit whatever you want (implement undo-operations, serialize the moves to a file, etc. etc.)