TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Adam
NA
131
30.6k
Checkers_03
May 4 2011 6:55 AM
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;
}
}
}
Reply
Answers (
32
)
Code SVN checkout by C#
Cannot Backup Of database