So here is code for .exe and below it for dll. When u start it, and press enter on some symbol and move it across another, the symbol u have moved across, disappear.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary1;
namespace Checkers
{
class Checkers
{
static Saveboard Board;
static char[,] Area;
static char[,] Area2;
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");
// restore cursor postion
Console.CursorTop = origRow;
Console.CursorLeft = origCol;
}
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);
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();
Area2 = Board.Checkerboard();
DrawBoard(true);
Console.ReadKey(true);
Keys();
}
}
}
Code for dll:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary1
{
public class Saveboard
{
const int X = 8;
const int Y = 8;
char[,] Area = new char[X, Y];
char[,] Area2 = new char[X, Y];
int switcher = 0;
char Symbol = (char)5;
char Symbol2 = (char)6;
bool pick = 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++;
}
}
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[2, 1] = Symbol;
Area[2, 3] = Symbol;
Area[2, 5] = Symbol;
Area[2, 7] = Symbol;
Area[4, 1] = Symbol2;
Area[4, 4] = Symbol2;
Area[5, 2] = Symbol2;
return Area;
}
public char[,] 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++;
}
return Area2;
}
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
{
Area[ypos, xpos] = Area2[ypos, xpos];
xpos++;
redraw = true;
Area[ypos, xpos] = Symbol;
}
col = xpos;
row = ypos;
return Area;
}
public char[,] MoveDown(out int col, out int row, out bool redraw)
{
if (ypos + 1 == Y)
{
redraw = true;
}
else if (pick == true)
{
ypos++;
redraw = true;
}
else
{
Area[ypos, xpos] = Area2[ypos, xpos];
ypos++;;
redraw = true;
Area[ypos, xpos] = Symbol;
}
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
{
Area[ypos, xpos] = Area2[ypos, xpos];
xpos--;
redraw = true;
Area[ypos, xpos] = Symbol;
}
col = xpos;
row = ypos;
return Area;
}
public char[,] MoveUp(out int col, out int row, out bool redraw)
{
if (ypos - 1 < 0)
{
redraw = true;
}
else if (pick == true)
{
ypos--;
redraw = true;
}
else
{
Area[ypos, xpos] = Area2[ypos, xpos];
ypos--;
Area[ypos, xpos] = Symbol;
redraw = true;
}
col = xpos;
row = ypos;
return Area;
}
public char[,] EnterPressed(out int col, out int row, out bool redraw)
{
if (pick == true && Area[ypos, xpos] == Symbol)
{
redraw = false;
pick = false;
}
else if (pick == false && Area2[ypos, xpos] != Symbol)
{
redraw = true;
Area[ypos, xpos] = Symbol;
pick = true;
}
else
redraw = false;
col = xpos;
row = ypos;
return Area;
}
}
}
Loading
VulpesPosted May 3, 2011, 7:01 PM
Again it would be a good idea to start a new thread and post the entire program (exe and dll) as it currently stands as you've added so many new variables that I'm now totally lost with it :(
AdamPosted May 3, 2011, 4:40 PM
AdamPosted May 3, 2011, 6:58 AM
if (xpos + 1 == X || xpos + 2 == X || ypos - 2 < 0)
{
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)
{
ypos--;
xpos++;
ypos--;
xpos++;
que4 = false;
que2 = false;
}
else
{
ypos--;
xpos++;
que2 = false;
}
2)Can you pls check my Save method because it doesnt work, but there are any error, only during Game, if I press 'S' it throws Error (Object reference not set to an instance of an object):
in .exe Ive added:
static Functions Func;
case ConsoleKey.S:
Area = Func.Save();
break;
In new DLL(Functions) is this:
using ClassLibrary1;
namespace ClassLibrary3
{
public class Functions : Saveboard
{
char[,] Area3 = new char[X, Y];
public char[,] Save()
{
for (int i = 0; i < X; i++)
{
for (int y = 0; y < Y; y++)
{
Area3[i, y] = Area[i, y];
}
}
return Area3;
}
}
}
VulpesPosted May 2, 2011, 6:21 PM
Even if (xpos +1 == X), you're still incrementing ypos and xpos which is going to cause an out of bounds exception.
This isn't an exception you want to catch - you need to prevent it happening in the first place.
As I said in my previous post, you won't need to rewrite the dll when you convert this application to Windows Forms but you will need to rewrite the .exe completely. As you'll probably be using the mouse rather than the keyboard to select pieces directly, you may need to add some methods to the dll but it's no good worrying about all that now - just try and finish the console application first.
AdamPosted May 2, 2011, 3:57 PM
VulpesPosted May 2, 2011, 3:00 PM
AdamPosted May 2, 2011, 11:32 AM
VulpesPosted May 2, 2011, 11:21 AM
I'd specify from the outset that player 1 will always play in one direction and player 2 in the other.
AdamPosted May 2, 2011, 8:31 AM
AdamPosted May 1, 2011, 2:44 PM
public char[,] MoveRight(out int col, out int row, out bool redraw) //just moves with cursor
{
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 (ypos + 1 == Y) // Ive tried to fix my problem
{
redraw = true;
}
else if (pick == true)
{
ypos++;
redraw = true;
}
else //pick = false
{
if (dir == true && arr == true) // when arr is true, it means that you can move this arrow only from start cursor position, because if you move with queen UP , you cant move down, till cursor will be at start position and all dir's are for moving with cursor, you are pressing Down arrow and cursor changes position when you cna move
{
if (xpos + 1 == X)
{
redraw = true;
}
else if (Area[ypos + 1, xpos + 1] == Symbol2)
{
ypos++;
xpos++;
dir4 = false;
}
ypos++;
xpos++;
dir = false;
dir2 = false;
}
else if (dir2 == false)
{
if (Area[ypos, xpos - 2] == Symbol2)
{
ypos++;
xpos--;
dir5 = false;
dir3 = false;
}
else if (dir4 == false)
{
ypos--;
xpos--;
dir4 = true;
}
xpos--;
xpos--;
dir2 = true;
dir3 = false;
}
else if (dir3 == false)
{
if (dir5 == false)
{
ypos--;
xpos++;
dir5 = true;
}
ypos--;
xpos++;
dir3 = true;
dir = true;
}
redraw = true;
}
col = xpos;
row = ypos;
return Area;
}
public char[,] MoveLeft(out int col, out int row, out bool redraw) //just moves with cursor
{
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 (ypos - 1 < 0)
{
redraw = true;
}
else if (pick == true)
{
ypos--;
redraw = true;
}
else
if(dir == true && queen == false) // when queen is false, program know that you want to move with queen
{
if (que == true) // que is similar to dir, just to other direction
{
if (Area[ypos - 1, xpos + 1] == Symbol2)
{
ypos--;
xpos++;
que4 = false;
}
ypos--;
xpos++;
arr = false;
que = false;
que2 = false;
}
else if (que2 == false)
{
if (Area[ypos, xpos - 2] == Symbol2)
{
ypos--;
xpos--;
que5 = false;
que3 = false;
}
else if (que4 == false)
{
ypos++;
xpos--;
dir4 = true;
}
xpos--;
xpos--;
arr = false;
que2 = true;
que3 = false;
}
else if (que3 == false)
{
if (que5 == false)
{
ypos++;
xpos++;
dir5 = true;
}
ypos++;
xpos++;
arr = true;
que3 = true;
que = true;
}
}// pick = 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 (pick == true && Area[ypos, xpos] == Symbol)
{
Area[ypos, xpos] = Area2[ypos, xpos];
redraw = true;
pick = false;
dir = true;
}
else if (pick == true && Area[ypos, xpos] == Symbol3)
{
Area[ypos, xpos] = Area2[ypos, xpos];
redraw = true;
pick = false;
dir = true;
que = true;
queen = false;
}
else if (pick == false && Area[ypos, xpos] == 'Y') // want to SAVE figurine
{
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;
}
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;
}
else if (que4 == false && (Area[ypos + 1, xpos - 1] == Symbol2))
{
Area[ypos + 1, xpos - 1] = Area2[ypos, xpos];
Area[ypos, xpos] = Symbol3;
}
else if (que5 == false && (Area[ypos - 1, xpos - 1] == Symbol2))
{
Area[ypos + 1, xpos - 1] = Area2[ypos, xpos];
Area[ypos, xpos] = Symbol3;
}
else if (ypos == 7 && Area[ypos, xpos] == 'Y')
{
Area[ypos, xpos] = Symbol3;
}
else
{
if (queen == false)
{
Area[ypos, xpos] = Symbol3;
queen = true;
}
else
Area[ypos, xpos] = Symbol;
}
queen = true; // after save position I set all to initial state
arr = true;
pick = true;
dir2 = true;
dir3 = true;
dir4 = true;
dir5 = true;
que2 = true;
que3 = true;
que4 = true;
que5 = true;
redraw = true;
}
else
redraw = false;
choose = pick;
col = xpos;
row = ypos;
return Area;
}
}
VulpesPosted May 1, 2011, 1:13 PM
AdamPosted May 1, 2011, 8:56 AM
else if (Area[ypos + 1, xpos + 1] == Symbol2)
{
ypos++;
xpos++;
dir4 = false;
}
But when Iam at e.g. Area[4,7] so it is a edge of board, and this condition has to look at position xpos + 1, what is out of board, it throws me exception. How can I write somethink like, try this: Area[ypos + 1, xpos + 1] = square in board, and if no, it moves with cursor e.g. xpos--; ?? Can I write it with try& catch some way?? thx
AdamPosted May 1, 2011, 5:55 AM
VulpesPosted Apr 30, 2011, 8:54 AM
AdamPosted Apr 30, 2011, 7:06 AM
public char[,] MoveUp(out int col, out int row, out bool redraw)
{
if (ypos - 1 < 0)
{
redraw = true;
}
else if (pick == true)
{
ypos--;
redraw = true;
}
else
{
if (dir == false && dir2 == false)
{
ypos--;
xpos++;
dir2 = true;
dir = true;
}
else if (dir == false)
{
ypos--;
xpos--;
dir = true;
}
redraw = true;
}
col = xpos;
row = ypos;
return Area;
}
AdamPosted Apr 30, 2011, 5:35 AM
VulpesPosted Apr 29, 2011, 5:52 PM
AdamPosted Apr 29, 2011, 3:09 PM
So when you look into MoveRight method in DLL, when pick = false and into it is dir = true. It couse that cursor moves (ypos++, xpos++) and dir= false. So you cant again press Right arrow. In method MoveUp , it again set dir to true, so you can press right again. And MoveLeft has dir2 extra. Because dir is only for move(ypos++;xpos++;)
and dir2 is for move (ypos++;xpos--;). But in MoveLeft are two 'dir's so when you move with figurine to area for example Area[6, 0] and than again left, but there is no more squares, and then Up + Right+Up, it moves cursor double xpos++, so I think that this is bcause of some mixed of true&false. And this is what I dont know how correct it. It is all because when pick = false, you can move with cursor only to position when you really can save symbol.
VulpesPosted Apr 29, 2011, 2:28 PM
What do 'dir' and 'dir2' signify and what are they set to initially?
AdamPosted Apr 29, 2011, 4:38 AM
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
{
if (dir == true)
{
ypos++;
xpos++;
dir = false;
}
redraw = true;
}
col = xpos;
row = ypos;
return Area;
}
public char[,] MoveDown(out int col, out int row, out bool redraw)
{
if (ypos + 1 == Y)
{
redraw = true;
}
else if (pick == true)
{
ypos++;
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
{
if (dir == true)
{
ypos++;
xpos--;
dir = false;
dir2 = false;
}
redraw = true;
}
col = xpos;
row = ypos;
return Area;
}
public char[,] MoveUp(out int col, out int row, out bool redraw)
{
if (ypos - 1 < 0)
{
redraw = true;
}
else if (pick == true)
{
ypos--;
redraw = true;
}
else
{
if (dir == false && dir2 == false)
{
ypos--;
xpos++;
dir2 = true;
dir = true;
}
else if (dir == false)
{
ypos--;
xpos--;
dir = true;
}
redraw = true;
}
col = xpos;
row = ypos;
return Area;
}
public char[,] EnterPressed(out int col, out int row, out bool redraw, out bool choose)
{
if (pick == true && Area[ypos, xpos] == Symbol)
{
Area[ypos, xpos] = Area2[ypos, xpos];
redraw = true;
pick = false;
dir = true;
}
else if (pick == false )
{
Area[ypos, xpos] = Symbol;
pick = true;
redraw = true;
}
else
redraw = false;
choose = pick;
col = xpos;
row = ypos;
return Area;
}
}
}
AdamPosted Apr 29, 2011, 3:40 AM
AdamPosted Apr 29, 2011, 3:12 AM
Below is piece of code of Enter when it 'works' but not at all, but I think it is good way to do:
else if (pick == false && Area[ypos, xpos] == 'Y')
{
if (ypos < 4)
{
Area[ypos, xpos] = Symbol;
pick = true;
redraw = true;
}
else
redraw = true;
But its good line only for 3 line on checkerboard, i dont know how can I write this: if figurine is from 3th line, then ypos has to be < 4. Have u got any idea how to write it please?? this is way how to do that but it is too concrete, dont u have any idea how similiar condition write for all figurine??
VulpesPosted Apr 28, 2011, 6:56 PM
AdamPosted Apr 28, 2011, 6:43 PM
I also want to thanks you a lot because you re really helping me and sometimes I think I bother you, so sorry but I have to done this game.
VulpesPosted Apr 28, 2011, 5:50 PM
AdamPosted Apr 28, 2011, 2:30 PM
VulpesPosted Apr 28, 2011, 2:17 PM
AdamPosted Apr 28, 2011, 1:15 PM
VulpesPosted Apr 28, 2011, 12:04 PM
if (pick == true && Area[ypos, xpos] == Symbol)
{
Area[ypos, xpos] = Area2[ypos, xpos];
redraw = true; // as we're changing pick
pick = false;
}
and when a redraw is done after pick is set to false, we'll need another change in the DrawBoard() method of the .exe:
Console.SetCursorPosition(0, 10); // now moved outside the 'if'
if (choose)
{
Console.WriteLine("Choose your figurine");
}
else
{
Console.WriteLine(" "); // remove prompt
}
VulpesPosted Apr 28, 2011, 11:52 AM
In the dll, introduce another 'out' parameter called 'choose' in the EnterPressed() method:
I think this is the only place where 'pick' can be changed so you shouldn't need to do anything for the arrow keys.
Now in the .exe, you'll need a few changes which I've highlighted as follows:
AdamPosted Apr 28, 2011, 9:16 AM
AdamPosted Apr 28, 2011, 5:22 AM
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++;
}
}
For all arrows the same change
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
{
xpos++;
redraw = true;
}
col = xpos;
row = ypos;
return Area;
}
public char[,] EnterPressed(out int col, out int row, out bool redraw)
{
if (pick == true && Area[ypos, xpos] == Symbol)
{
Area[ypos, xpos] = Area2[ypos, xpos];
redraw = false;
pick = false;
}
else if (pick == false && Area[ypos, xpos] == 'Y')
{
redraw = true;
Area[ypos, xpos] = Symbol;
pick = true;
}
else
redraw = false;
col = xpos;
row = ypos;
return Area;
}
VulpesPosted Apr 27, 2011, 3:30 PM
AdamPosted Apr 27, 2011, 12:43 PM
VulpesPosted Apr 27, 2011, 12:07 PM
Checkerboard(); // insert this line
AdamPosted Apr 27, 2011, 11:48 AM
void Checkerboard()
{
Cursor();
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++;
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[2, 1] = Symbol;
Area[2, 3] = Symbol;
Area[2, 5] = Symbol;
Area[2, 7] = Symbol;
Area[4, 1] = Symbol2;
Area[4, 4] = Symbol2;
Area[5, 2] = Symbol2;
Console.Write(Area[i, y]);
}
Console.WriteLine(num);
num--;
switcher++;
}
Console.WriteLine("ABCDEFGH");
}
void Checkerboard2()
{
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++;
}
}
void Cursor()
{
bool saveCursorVisibile;
int saveCursorSize;
Console.CursorVisible = true;
saveCursorVisibile = Console.CursorVisible;
saveCursorSize = Console.CursorSize;
Console.CursorSize = 100;
}
void Keys()
{
while (true)
{
var ch = Console.ReadKey().Key;
switch (ch)
{
case ConsoleKey.Enter:
{
if (Area[ypos, xpos] == Symbol && pick == false)
{
pick = true;
Area[ypos, xpos] = Area2[ypos, xpos];
}
else if (Area[ypos, xpos] != Symbol && pick == true)
{
Area[ypos, xpos] = Symbol;
pick = false;
}
break;
}
case ConsoleKey.UpArrow:
{
if (ypos - 1 < 0)
{
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
break;
}
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
ypos--;
break;
}
case ConsoleKey.DownArrow:
{
if (ypos + 1 == Y)
{
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
break;
}
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
ypos++;
break;
}
case ConsoleKey.LeftArrow:
{
if (xpos - 1 < 0)
{
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
break;
}
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
xpos--;
break;
}
case ConsoleKey.RightArrow:
{
if (xpos + 1 == X)
{
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
break;
}
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
xpos++;
break;
}
}
Console.SetCursorPosition(xpos, ypos);
if (pick == true)
{
Console.Write(Symbol);
Console.SetCursorPosition(xpos, ypos);
}
}
VulpesPosted Apr 27, 2011, 7:15 AM