Thanks for Save() and Load() methods. They work good but when I save somethink and Load it, it is loaded after press some key... and How would you do method StepBack ?? I didnt tell u before because I dont know about it:D ok so I will just do Binary seialization, and could I later do also Events?? because it all will be also for Windows Form. Can you tell me about some good tutorial for Windows Form after which I ll be able to remake dll also for WF?? And what about exceptions??. The rest I wont do.Below is whole code. You already can play checker, I just must finish a couple of details. THX!
.exe:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary1;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Checkers
{
class Checkers
{
static Saveboard Board;
static char[,] Area;
static bool choose = true;
static void DrawBoard(bool firstTime)
{
//only need to do some things the first time the board is drawn
const int X = 8;
const int Y = 8;
int num = 8;
if (firstTime) Console.Clear();
// save cursor position
int origRow = Console.CursorTop;
int origCol = Console.CursorLeft;
// reset to 0,0
Console.SetCursorPosition(0, 0);
for (int i = 0; i < X; i++)
{
for (int y = 0; y < Y; y++)
{
Console.Write(Area[i, y]);
}
if (firstTime)
{
Console.WriteLine(num);
num--;
}
else
{
Console.WriteLine();
}
}
if (firstTime) Console.WriteLine("ABCDEFGH");
Console.SetCursorPosition(0, 10);
if(choose)
Console.WriteLine("Player 1");
else
Console.WriteLine("Player 2");
// restore cursor postion
Console.CursorTop = origRow;
Console.CursorLeft = origCol;
}
static void Save()
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream fs = File.Create("saveboard.bin"))
formatter.Serialize(fs, Board);
}
static void Load()
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream fs = File.OpenRead("saveboard.bin"))
Board = (Saveboard)formatter.Deserialize(fs);
}
public static void Keys()
{
bool redraw = false;
int col = 0;
int row = 0;
while (true)
{
var ch = Console.ReadKey().Key;
switch (ch)
{
case ConsoleKey.RightArrow:
Area = Board.MoveRight(out col, out row, out redraw);
break;
case ConsoleKey.DownArrow:
Area = Board.MoveDown(out col, out row, out redraw);
break;
case ConsoleKey.LeftArrow:
Area = Board.MoveLeft(out col, out row, out redraw);
break;
case ConsoleKey.UpArrow:
Area = Board.MoveUp(out col, out row, out redraw);
break;
case ConsoleKey.Enter:
Area = Board.EnterPressed(out col, out row, out redraw, out choose);
break;
case ConsoleKey.S:
Save();
break;
case ConsoleKey.L:
Load();
break;
}
if (redraw) DrawBoard(false);
Console.SetCursorPosition(col, row); // in case it's not in the right position following the move
}
}
static void Main()
{
Board = new Saveboard();
Area = Board.InitializeBoard();
DrawBoard(true);
Console.ReadKey(true);
Keys();
}
}
}
DLL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary1
{
public class Saveboard
{
public const int X = 8;
public const int Y = 8;
public char[,] Area = new char[X, Y];
char[,] Area2 = new char[X, Y];
int switcher = 0;
char Symbol = (char)5;
char Symbol2 = (char)6;
char Symbol3 = (char)4;
char Symbol4 = (char)3;
bool pick = true;
bool dir = true;
bool dir2 = true;
bool dir3 = true;
bool dir4 = true;
bool dir5 = true;
bool dir6 = true;
bool queen = true;
bool que = true;
bool que2 = true;
bool que3 = true;
bool que4 = true;
bool que5 = true;
bool que6 = true;
bool arr = true;
bool again = true;
bool player = true;
int xpos = 0;
int ypos = 0;
public char[,] InitializeBoard()
{
for (int i = 0; i < X; i++)
{
for (int y = 0; y < Y; y++)
{
if (switcher % 2 == 0)
Area[i, y] = 'X';
else
Area[i, y] = 'Y';
switcher++;
}
switcher++;
}
Area[0, 1] = Symbol;
Area[0, 3] = Symbol;
Area[0, 5] = Symbol;
Area[0, 7] = Symbol;
Area[1, 0] = Symbol;
Area[1, 2] = Symbol;
Area[1, 4] = Symbol;
Area[1, 6] = Symbol;
Area[7, 0] = Symbol2;
Area[7, 2] = Symbol2;
Area[7, 4] = Symbol2;
Area[7, 6] = Symbol2;
Area[6, 1] = Symbol2;
Area[6, 3] = Symbol2;
Area[6, 5] = Symbol2;
Area[6, 7] = Symbol2;
/*Area[4, 3] = Symbol;
Area[5, 2] = Symbol2;
Area[5, 4] = Symbol2;*/
Checkerboard();
return Area;
}
void Checkerboard()
{
for (int i = 0; i < X; i++)
{
for (int y = 0; y < Y; y++)
{
if (switcher % 2 == 0)
Area2[i, y] = 'X';
else
Area2[i, y] = 'Y';
switcher++;
}
switcher++;
}
}
public char[,] MoveRight(out int col, out int row, out bool redraw)
{
if (xpos + 1 == X)
{
redraw = true;
}
else if (pick)
{
xpos++;
redraw = true;
}
else
redraw = true;
col = xpos;
row = ypos;
return Area;
}
public char[,] MoveDown(out int col, out int row, out bool redraw)
{
if (player == true)
{
if (ypos + 1 == Y)
{
redraw = true;
}
else if (pick == true)
{
ypos++;
redraw = true;
}
else //pick = false
{
if (dir == true && arr == true)
{
if (xpos + 1 == X)
{
if (Area[ypos + 1, xpos - 1] == Symbol2)
{
ypos++; xpos--;
dir5 = false;
}
ypos++; xpos--;
dir3 = false;
dir2 = true;
}
else if (Area[ypos + 1, xpos + 1] == Symbol2)
{
if (xpos + 2 == X)
{
if (Area[ypos + 1, xpos - 1] == Symbol2)
{
ypos++;
xpos--;
dir5 = false;
}
ypos++;
xpos--;
dir3 = false;
dir2 = true;
}
else
{
ypos++;
xpos++;
ypos++;
xpos++;
dir4 = false;
dir2 = false;
}
}
else
{
ypos++;
xpos++;
dir2 = false;
}
again = true;
dir = false;
}
else if (dir2 == false)
{
if (xpos - 2 < 0 )
{
ypos--; xpos--;
dir3 = true;
dir = true;
}
else if (dir4 == false)
{
if (xpos - 3 < 0)
{
xpos++; ypos--;
dir = true;
dir3 = true;
}
else if (Area[ypos - 1, xpos - 3] == Symbol2)/////////////////////////
{
if (xpos - 4 < 0)
{
xpos++; ypos--;
dir = true;
dir3 = true;
}
else
{
ypos++;
xpos--;
dir6 = false;
dir3 = false;
}
}
ypos--;
xpos--;
xpos--;
xpos--;
dir3 = false;
dir4 = true;
}
else if (Area[ypos, xpos - 2] == Symbol2)
{
if (xpos - 3 < 0)
{
xpos--; ypos--;
dir = true;
dir3 = true;
}
else
{
ypos++;
xpos--;
xpos--;
xpos--;
dir5 = false;
dir3 = false;
}
}
else
{
xpos--;
xpos--;
dir3 = false;
}
again = true;
dir2 = true;
}
else if (dir3 == false)
{
if (dir5 == false)
{
ypos--;
xpos++;
dir5 = true;
}
else if (dir6 == false)
{
ypos--;
xpos++;
dir6 = true;
}
ypos--;
xpos++;
again = false;
dir3 = true;
dir = true;
}
redraw = true;
}
}
//************************** PLAYER 2 ********************************** MOVE DOWN
else if (player == false)
{
if (ypos + 1 == Y)
{
redraw = true;
}
else if (pick == true)
{
ypos++;
redraw = true;
}
else //pick = false
{
if (que == true && queen == false)
{
if (dir == true)
{
if (xpos + 1 == X)
{
if (Area[ypos + 1, xpos - 1] == Symbol)
{
ypos++; xpos--;
dir5 = false;
}
xpos--;
ypos++;
dir3 = false;
dir2 = true;
redraw = true;
}
else if (Area[ypos + 1, xpos + 1] == Symbol)
{
if (xpos + 2 == X)
{
if (Area[ypos + 1, xpos - 1] == Symbol)
{
ypos++;
xpos--;
dir5 = false;
}
ypos--;
xpos--;
dir3 = false;
dir2 = true;
}
else
{
ypos++;
xpos++;
ypos++;
xpos++;
dir4 = false;
dir2 = false;
}
}
else { xpos++; ypos++; dir2 = false; }
again = true;
arr = false;
dir = false;
}
else if (dir2 == false)
{
if (xpos - 2 < 0 || xpos - 3 < 0)
{
ypos--; xpos--;
dir3 = true;
dir = true;
}
else if (dir4 == false)
{
if (xpos - 3 < 0)
{
xpos++; ypos--;
dir = true;
dir3 = true;
}
else if (Area[ypos - 1, xpos - 3] == Symbol)
{
if (xpos - 4 < 0)
{
xpos++; ypos--;
dir = true;
dir3 = true;
}
else
{
xpos--;
ypos++;
dir6 = false;
dir3 = false;
}
}
ypos--;
xpos--;
xpos--;
xpos--;
dir4 = true;
}
else if (Area[ypos, xpos - 2] == Symbol)
{
if (xpos - 3 < 0)
{
xpos--; ypos--;
dir = true;
dir3 = true;
}
else
{
ypos++;
xpos--;
xpos--;
xpos--;
dir5 = false;
dir3 = false;
}
}
else
{
xpos--;
xpos--;
dir3 = false;
}
again = true;
arr = false;
dir2 = true;
}
else if (dir3 == false)
{
if (dir5 == false)
{
ypos--;
xpos++;
dir5 = true;
}
else if (dir6 == false)
{
xpos++;
ypos--;
dir6 = true;
}
ypos--;
xpos++;
arr = true;
dir3 = true;
dir = true;
again = false;
}
}
} redraw = true;
}
else
redraw = true;
col = xpos;
row = ypos;
return Area;
}
public char[,] MoveLeft(out int col, out int row, out bool redraw)
{
if (xpos - 1 < 0 )
{
redraw = true;
}
else if (pick == true)
{
xpos--;
redraw = true;
}
else
redraw = true;
col = xpos;
row = ypos;
return Area;
}
public char[,] MoveUp(out int col, out int row, out bool redraw)
{
if (player == true)
{
if (ypos - 1 < 0)
{
redraw = true;
}
else if (pick == true)
{
ypos--;
redraw = true;
}
else
if (dir == true && queen == false)
{
if (que == true)
{
if (xpos + 1 == X) /// 2
{
if (Area[ypos - 1, xpos - 1] == Symbol2)
{
ypos--; xpos--;
que5 = false;
}
ypos--;
xpos--;
que2 = true;
que3 = false;
}
else if (Area[ypos - 1, xpos + 1] == Symbol2)
{
if (xpos + 2 == X)
{
if (Area[ypos - 1, xpos - 1] == Symbol2)
{
ypos--;
xpos--;
que5 = false;
}
ypos--; xpos--;
que2 = true;
que3 = false;
}
else
{
ypos--;
xpos++;
ypos--;
xpos++;
que4 = false;
que2 = false;
}
}
else
{
ypos--;
xpos++;
que2 = false;
}
again = true;
arr = false;
que = false;
}
else if (que2 == false)
{
if (xpos - 2 < 0) // 1
{
ypos++; xpos--;
que = true;
que3 = true;
}
else if (que4 == false)
{
if (xpos - 3 < 0) // 3
{
xpos++; ypos++;
que = true;
que3 = true;
}
else if (Area[ypos - 1, xpos - 3] == Symbol2)
{
if (xpos - 3 < 0)
{
ypos--; xpos--;
que = true;
que3 = true;
}
else if (xpos - 4 < 0)
{
ypos++; xpos++;
que = true;
que3 = true;
}
else
{
ypos++;
xpos--;
que6 = false;
que3 = false;
}
}
ypos++;
xpos--;
xpos--;
xpos--;
que3 = false;
que4 = true;
}
else if (Area[ypos, xpos - 2] == Symbol2)
{
if (xpos - 3 < 0)
{
xpos--; ypos++;
que = true;
que3 = true;
}
else
{
ypos--;
xpos--;
xpos--;
xpos--;
que5 = false;
que3 = false;
}
}
else
{
xpos--;
xpos--;
que3 = false;
}
again = true;
arr = false;
que2 = true;
}
else if (que3 == false)
{
if (que5 == false)
{
ypos++;
xpos++;
dir5 = true;
}
else if (que6 == false)
{
ypos++;
xpos++;
que6 = true;
}
ypos++;
xpos++;
arr = true;
que3 = true;
que = true;
again = false;
}
}// pick = false
}
//************************************* player 2 ************************** UP ARROW
else if (player == false)
{
if (ypos - 1 < 0)
{
redraw = true;
}
else if (pick == true)
{
ypos--;
redraw = true;
}
else // pick = false
if (que == true && arr == true)
{
if (xpos + 1 == X ) // 2 ZMENA
{
if (Area[ypos - 1, xpos - 1] == Symbol)
{
ypos--; xpos--;
que5 = false;
}
ypos--;
xpos--;
que2 = true;
que3 = false;
}
else if (Area[ypos - 1, xpos + 1] == Symbol)
{
if (xpos + 2 == X)
{
if (Area[ypos - 1, xpos - 1] == Symbol)
{
ypos--;
xpos--;
que5 = false;
}
ypos--; xpos--;
que2 = true;
que3 = false;
}
else
{
ypos--;
xpos++;
ypos--;
xpos++;
que4 = false;
que2 = false;
}
}
else
{
ypos--;
xpos++;
que2 = false;
}
again = true;
que = false;
}
else if (que2 == false)
{
if (xpos - 2 < 0 ) // 1 ZMENA
{
ypos++; xpos--;
que = true;
que3 = true;
}
else if (que4 == false)
{
if (xpos - 3 < 0) // 3 ZMENA
{
xpos++; ypos++;
que = true;
que3 = true;
}
else if (Area[ypos + 1, xpos - 3] == Symbol)
{
if (xpos - 3 < 0)
{
ypos--; xpos--;
que = true;
que3 = true;
}
else if (xpos - 4 < 0)
{
ypos++; xpos++;
que = true;
que3 = true;
}
else
{
ypos--;
xpos--;
que6 = false;
que3 = false;
}
}
ypos++;
xpos--;
xpos--;
xpos--;
que3 = false;
que4 = true;
}
else if (Area[ypos, xpos - 2] == Symbol)
{
if (xpos - 3 < 0)
{
xpos--; ypos++;
que = true;
que3 = true;
}
else
{
ypos--;
xpos--;
xpos--;
xpos--;
que5 = false;
que3 = false;
}
}
else
{
xpos--;
xpos--;
que3 = false;
}
again = true;
que2 = true;
}
else if (que3 == false)
{
if (que5 == false)
{
ypos++;
xpos++;
que5 = true;
}
else if (que6 == false)
{
ypos++;
xpos++;
que6 = true;
}
ypos++;
xpos++;
que3 = true;
que = true;
again = false;
}
}
redraw = true;
col = xpos;
row = ypos;
return Area;
}
public char[,] EnterPressed(out int col, out int row, out bool redraw, out bool choose)
{
if (player == true)
{
if (pick == true && Area[ypos, xpos] == Symbol)
{
Area[ypos, xpos] = Area2[ypos, xpos];
redraw = true;
pick = false;
dir = true;
again = false;
}
else if (pick == true && Area[ypos, xpos] == Symbol3)
{
Area[ypos, xpos] = Area2[ypos, xpos];
redraw = true;
pick = false;
dir = true;
que = true;
queen = false;
again = false;
}
else if (pick == false && Area[ypos, xpos] == 'Y') // SAVE
{
if (dir5 == false && (Area[ypos - 1, xpos + 1] == Symbol2))
{
Area[ypos - 1, xpos + 1] = Area2[ypos, xpos];
if (queen == false)
Area[ypos, xpos] = Symbol3;
else
Area[ypos, xpos] = Symbol;
player = false;
}
else if (dir4 == false && (Area[ypos - 1, xpos - 1] == Symbol2))
{
Area[ypos - 1, xpos - 1] = Area2[ypos, xpos];
if (queen == false)
Area[ypos, xpos] = Symbol3;
else
Area[ypos, xpos] = Symbol;
player = false;
}
else if (dir6 == false && (Area[ypos - 1, xpos + 1] == Symbol2))
{
Area[ypos - 1, xpos + 1] = Area2[ypos, xpos];
if (queen == false)
Area[ypos, xpos] = Symbol3;
else
Area[ypos, xpos] = Symbol;
player = false;
}
else if (que4 == false && (Area[ypos + 1, xpos - 1] == Symbol2))
{
Area[ypos + 1, xpos - 1] = Area2[ypos, xpos];
Area[ypos, xpos] = Symbol3;
player = false;
}
else if (que5 == false && (Area[ypos + 1, xpos + 1] == Symbol2))
{
Area[ypos + 1, xpos + 1] = Area2[ypos, xpos];
Area[ypos, xpos] = Symbol3;
player = false;
}
else if (que6 == false && (Area[ypos + 1, xpos + 1] == Symbol2))
{
Area[ypos + 1, xpos + 1] = Area2[ypos, xpos];
Area[ypos, xpos] = Symbol3;
player = false;
}
else if (ypos == 7 && Area[ypos, xpos] == 'Y')
{
Area[ypos, xpos] = Symbol3;
}
else if (again == false)
{
if (queen == false)
{
Area[ypos, xpos] = Symbol3;
queen = true;
}
else
Area[ypos, xpos] = Symbol;
player = true;
}
else
{
if (queen == false)
{
Area[ypos, xpos] = Symbol3;
queen = true;
}
else
Area[ypos, xpos] = Symbol;
player = false;
}
queen = true;
arr = true;
player = false;
pick = true;
dir = true;
dir2 = true;
dir3 = true;
dir4 = true;
dir5 = true;
que2 = true;
que3 = true;
que4 = true;
que5 = true;
again = true;
redraw = true;
}
else
redraw = false;
}
//*************************************** PLAYER 2 ************************** ENTER
else if (player == false)
{
if (pick == true && Area[ypos, xpos] == Symbol2)
{
Area[ypos, xpos] = Area2[ypos, xpos];
redraw = true;
pick = false;
que = true;
again = false;
}
else if (pick == true && Area[ypos, xpos] == Symbol4)
{
Area[ypos, xpos] = Area2[ypos, xpos];
redraw = true;
pick = false;
dir = true;
que = true;
queen = false;
}
else if (pick == false && Area[ypos, xpos] == 'Y') // SAVE
{
if (que5 == false && (Area[ypos + 1, xpos + 1] == Symbol))
{
Area[ypos + 1, xpos + 1] = Area2[ypos, xpos];
if (queen == false)
Area[ypos, xpos] = Symbol4;
else
Area[ypos, xpos] = Symbol2;
player = false;
}
else if (que4 == false && (Area[ypos + 1, xpos - 1] == Symbol))
{
Area[ypos + 1, xpos - 1] = Area2[ypos, xpos];
if (queen == false)
Area[ypos, xpos] = Symbol4;
else
Area[ypos, xpos] = Symbol2;
player = false;
}
else if (que6 == false && (Area[ypos + 1, xpos + 1] == Symbol))
{
Area[ypos + 1, xpos + 1] = Area2[ypos, xpos];
if (queen == false)
Area[ypos, xpos] = Symbol4;
else
Area[ypos, xpos] = Symbol2;
player = false;
}
else if (dir4 == false && (Area[ypos - 1, xpos - 1] == Symbol))
{
Area[ypos - 1, xpos - 1] = Area2[ypos, xpos];
Area[ypos, xpos] = Symbol4;
player = false;
}
else if (dir5 == false && (Area[ypos - 1, xpos - 1] == Symbol))
{
Area[ypos - 1, xpos - 1] = Area2[ypos, xpos];
Area[ypos, xpos] = Symbol4;
player = false;
}
else if (dir6 == false && (Area[ypos - 1, xpos + 1] == Symbol))
{
Area[ypos - 1, xpos + 1] = Area2[ypos, xpos];
Area[ypos, xpos] = Symbol4;
player = false;
}
else if (ypos == 0 && Area[ypos, xpos] == 'Y')
{
Area[ypos, xpos] = Symbol4;
}
else if (again == false)
{
if (queen == false)
{
Area[ypos, xpos] = Symbol4;
queen = true;
}
else
Area[ypos, xpos] = Symbol2;
player = false;
}
else
{
if (queen == false)
{
Area[ypos, xpos] = Symbol4;
queen = true;
}
else
Area[ypos, xpos] = Symbol2;
player = true;
}
queen = true;
pick = true;
player = true;
dir = true;
arr = true;
dir2 = true;
dir3 = true;
dir4 = true;
dir5 = true;
que2 = true;
que3 = true;
que4 = true;
que5 = true;
again = true;
redraw = true;
}
else
redraw = true;
}
choose = player;
col = xpos;
row = ypos;
redraw = true;
return Area;
}
}
}
Loading
AdamPosted May 17, 2011, 7:31 AM
VulpesPosted May 17, 2011, 6:50 AM
VulpesPosted May 17, 2011, 5:24 AM
AdamPosted May 17, 2011, 3:06 AM
VulpesPosted May 15, 2011, 3:37 PM
However, if MoveUp works but MoveDown doesn't, then I'd put the code for the two methods side by side and go through both methods line by line until you find the problem as the code for the two methods should be complimentary.
It's not possible to change the border color of the standard picturebox. What you should do instead is to use different images for highlighted squares - just use the same images as for non-highlighted squares but with a different background color.
Regarding the third point:
Add this class to the dll within the same namespace as the SaveBoard class:
public class Move
{
public char[,] Area = new char[X, Y];
public bool pick;
public bool dir;
public bool dir3;
public bool dir4;
public bool dir5;
public bool dir6;
public bool queen;
public bool que;
public bool que2;
public bool que3;
public bool que4;
public bool que5;
public bool que6;
public bool arr;
public bool again;
public bool player;
public int xpos;
public int ypos;
}
Add the following field to the SaveBoard class (just after ypos):
private Stack
Create a new method in the SaveBoard class:
private void SaveMove();
{
Move m = new Move();
m.Area = Area;
m.pick = pick;
m.dir = dir; // etc down to ypos
moves.Push(m);
}
Call the SaveMove() method before every new move.
Now add a MoveBack Method to the SaveBoard class:
public void MoveBack()
{
if (moves.Count > 0)
{
Move m = moves.Pop();
Area = m.Area;
pick = m.Pick;
dir = m.dir;
}
return Area;
and call that from within SaveBoard whenever you want to move back. Area etc. should then be passed back to the exe in the normal way so that the board can be redrawn.
AdamPosted May 15, 2011, 9:37 AM
VulpesPosted May 14, 2011, 2:17 PM
AdamPosted May 14, 2011, 7:47 AM
it throws error that it could not find T type, so maybe I shoud use som 'using'. Pleace correct me if I understand it wrong. So I ll creat Stack in Main method and in form. then I moves.Push(Area) only when choose is false or true ?? because choose is changing just when I need put Area into stack. Or should I do some method like:
public char[,] Undo()
{
for (int i = 0; i < X; i++)
{
for (int y = 0; y < Y; y++)
{
Area[i, y] = Area3[i, y];
}
}
return Area3;
}
and Push Area3 into stack?? then what should I do when Stack is full? I d clear all stack and start again.
About iteratr, why should I convert stack from 1,2,3 to 3,2,1?? I want move only one step back So if I put moves into stack 1,2,3, the last move will still be on the top. And I dont know different between iterator and foreach. Can you show me please how could it look?I dont know how transfer my idea to code:(
VulpesPosted May 10, 2011, 3:36 PM
For generics and generic collections, see here (http://msdn.microsoft.com/en-us/library/512aeb7t.aspx), here (http://msdn.microsoft.com/en-us/library/ms172181.aspx) and also here (http://en.csharp-online.net/Understanding_Generics%E2%80%94Creating_a_Custom_Generic_Collection).
I'd also search the articles on this site as there are sure to be some good ones on these topics.
AdamPosted May 10, 2011, 3:23 PM
VulpesPosted May 10, 2011, 3:14 PM
AdamPosted May 10, 2011, 2:17 PM
VulpesPosted May 10, 2011, 5:20 AM
When adding events to forms and controls in Visual Studio, it's best to always do so from the Properties box. Click on the 'lightning bolt' symbol to examine events rather than properties, find the event you need and then double click it to open up a skeleton eventhandler into which you can insert your code. When you do it this way, VS automatically wires up the event for you.
Assuming you haven't done this, then add this line before any others in Form1_Load:
this.KeyDown += Form1_KeyDown;
to wire it up manually.
PS: Also don't forget to set the form's KeyPreview property to true.
AdamPosted May 10, 2011, 5:09 AM
VulpesPosted May 10, 2011, 5:02 AM
AdamPosted May 10, 2011, 4:22 AM
case (char)5:
return Image.FromFile ("symbol1.bmp");
FileNotFoundException was unhandled I also try put there path to symbol1.bmp but it was the same
VulpesPosted May 9, 2011, 6:21 PM
When you deploy the application, you could include them as a resource but I wouldn't worry about that just now.
AdamPosted May 9, 2011, 4:31 PM
VulpesPosted May 9, 2011, 4:08 PM
AdamPosted May 9, 2011, 3:21 PM
Can you please describe what highlighted lines do?? And where should I save my images?? symbol1.bmp etc.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private const int X = 8;
private const int Y = 8;
private Saveboard Board;
private bool choose = true;
private char[,] Area;
private PictureBox[,] squares;
private int prevCol, prevRow;
string filePath = @"C:\Users\expire\Documents\Visual Studio 2010\Projects\Checkers\Checkers\bin\Debug\saveboard.bin"; // use full path here , and shou I load saveboard.bin if my DLL is called ClassLibrary1?
private void Form1_Load(object sender, EventArgs e) // why this method is here since Form existing
{
squares = new PictureBox[8, 8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
string num = (i * 8 + j + 1).ToString();
PictureBox pb = (PictureBox)this.Controls["pictureBox" + num];
squares[i, j] = pb;
}
}
// highlight first one
squares[0, 0].BorderStyle = BorderStyle.Fixed3D; // or whatever method you're using to highlight
LoadGame(); // not using Load as conflicts with an existing form member
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int col = 0 , row = 0;
bool redraw = false;
switch (e.KeyCode)
{
case Keys.Right:
Area = Board.MoveRight(out col, out row, out redraw);
break;
case Keys.Down:
Area = Board.MoveDown(out col, out row, out redraw);
break;
case Keys.Left:
Area = Board.MoveLeft(out col, out row, out redraw);
break;
case Keys.Up:
Area = Board.MoveUp(out col, out row, out redraw);
break;
case Keys.Enter:
Area = Board.EnterPressed(out col, out row, out redraw, out choose);
break;
case Keys.S:
Save();
redraw = false;
e.Handled = true;
return;
case Keys.Q:
e.Handled = true;
Close();
return;
default:
return;
}
e.Handled = true;
if (redraw) DrawBoard(false);
// as there's no cursor now, need this code
if (col != prevCol || row != prevRow)
{
squares[prevRow, prevCol].BorderStyle = BorderStyle.FixedSingle;
squares[row, col].BorderStyle = BorderStyle.Fixed3D;
prevRow = row;
prevCol = col;
}
}
private void DrawBoard(bool firstTime)
{
//only need to do some things the first time the board is drawn
int num = 8;
for (int i = 0; i < X; i++)
{
for (int y = 0; y < Y; y++)
{
squares[i, y].Image = GetImage(i, y);
}
// not sure what you're doing about printing numbers
}
// not sure what you're doing about printing letters
if(choose)
label1.Text = "Player 1";
else
label1.Text = "Player 2";
}
private Image GetImage(int col, int row)
{
// may be a good idea to load all these images initially into an Image[] and then just return an element of this array
switch (Area[col, row])
{
case (char)5:
return Image.FromFile ("symbol1.bmp"); // or whatever
case (char)6:
return Image.FromFile ("symbol2.bmp");
case (char)4:
return Image.FromFile ("symbol3.bmp");
case (char)3:
return Image.FromFile ("symbol4.bmp");
default:
return null;
}
}
private void Save()
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream fs = File.Create(filePath))
formatter.Serialize(fs, Board);
}
private void LoadGame()
{
if (File.Exists(filePath))
{
DialogResult result = MessageBox.Show("Do you want to load the previous game?", "Load Game", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream fs = File.OpenRead(filePath))
Board = (Saveboard)formatter.Deserialize(fs);
int col, row;
Area = Board.ReloadBoard(out col, out row, out choose);
if (col != prevCol || row != prevRow)
{
squares[prevRow, prevCol].BorderStyle = BorderStyle.FixedSingle;
squares[row, col].BorderStyle = BorderStyle.Fixed3D;
prevRow = row;
prevCol = col;
}
else
{
Board = new Saveboard();
Area = Board.InitializeBoard();
}
}
else
{
Board = new Saveboard();
Area = Board.InitializeBoard();
}
DrawBoard(true);
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
DialogResult result = MessageBox.Show("Do you want to save the state of the game?", "Save Game", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes) Save();
}
}
VulpesPosted May 9, 2011, 2:25 PM
AdamPosted May 9, 2011, 7:36 AM
VulpesPosted May 8, 2011, 2:12 PM
You'll need to set the Form's KeyPreview property to true and handle its KeyDown event so that you can trap all key presses made by the user. As before you'll need to switch on the key pressed and call the appropriate method in the dll.
You'll need a method to paint the board i.e. a grid of 64 pictureboxes, populate them with the appropriate images and highlight the current square.
You can try it but I think it'll be too slow to repaint the board after every move and you may therefore need to find some way to repaint only those squares which have changed. To do that you could iterate through the array returned by the dll and compare it with the Area array that was returned after the previous move.
AdamPosted May 8, 2011, 1:17 PM
VulpesPosted May 4, 2011, 6:56 PM
You can then load in images of your figurines and move them from box to box.
AdamPosted May 4, 2011, 6:21 PM
VulpesPosted May 4, 2011, 6:05 PM
Frankly, the custom exception is artificial but it may be the only opportunity you'll have to put one into the program. Although nobody would do it like this in practice, they probably would place a 'normal' try/catch around any code involving disk access which can go wrong for a variety of reasons so at least it'll save you the bother of that :)
AdamPosted May 4, 2011, 5:48 PM
VulpesPosted May 4, 2011, 5:22 PM
To enable 'stepback' in the game, you'll need to 'remember' each move so you can retrace it if necessary. That's a matter of storing all the relevant variables so that the board can be restored to its former state. How you do it will depend on how many moves you want to store. If it's only one, you could save the board to a temporary disk file before each move or, if it's a small fixed number, you could have a corresponding number of disk files which are overwritten as necessary and then deleted completely when the game ends.
I've modified the Checkers class to accommodate a custom exception class which should be compiled with the .exe. Here's the code:
As you'll see there's some test code in the Save() and Load() methods which is just used to simulate an exception. It should be removed when you're sure it's working properly.
AdamPosted May 4, 2011, 4:47 PM
VulpesPosted May 4, 2011, 3:05 PM
I'll see if I can come up with a custom exception class that you can use when loading and saving the state of the game.
VulpesPosted May 4, 2011, 2:32 PM
What happens now is that, if the file saveboard.bin exists on disk, the user is asked on starting the application whether (s)he wants to reload the previous game. Also, as well as the 'S' option to save the state of the game during play, I've also added a 'Q' option to quit when the user is then given the option to save the game prior to termination. I've removed the 'L' option as it's not the right place for it and you don't need it now anyway.
I've also incorporated an event (CancelKeyPress) into the Checkers class to deal with someone pressing Control-C when the application is running. This in fact is the only event which the Console class supports:
The only change needed to the dll is to add this method:
I'll see if I can find you a decent online tutorial on Windows Forms though I'll warn you now - it's not something you can pick up in 5 minutes.