Hi, I need advice for my game checkers please. Below is my code. When you pull enter you choose figurine and you can move with it on checkerboard and then save it to new position. It worked good with figurine on start position Area[0,0], but when I add new figurine on position Area[0,2] it disappear when cursor crosses it. Why it works only with figurine on position [0,0] ??
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Checkers
{
class Checkers
{
const int X = 8;
const int Y = 8;
int switcher = 0;
int num = 8;
int xpos = 0;
int ypos = 0;
bool pick = false;
char[,] Area = new char[X, Y];
char[,] Area2 = new char[X, Y];
char Symbol = (char)5;
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, 0] = Symbol;
Area[0, 2] = Symbol;
Console.Write(Area[i, y]);
}
Console.WriteLine(num);
num--;
switcher++;
}
Console.WriteLine("ABCDEFGH");
if (pick == false)
Console.Write("Vyber figurku");
if(pick==true)
Console.Write("Vyber misto a uloz");
}
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[xpos, ypos] == Symbol && pick == false)
{
pick = true;
}
if (Area[xpos, ypos] != Symbol && pick == true)
{
Area[xpos, ypos] = Symbol;
pick = false;
}
break;
case ConsoleKey.UpArrow:
{
if (ypos - 1 < 0)
break;
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
ypos--;
break;
}
case ConsoleKey.DownArrow:
{
if (ypos + 1 == Y)
break;
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
ypos++;
break;
}
case ConsoleKey.LeftArrow:
{
if (xpos - 1 < 0)
break;
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
xpos--;
break;
}
case ConsoleKey.RightArrow:
{
if (xpos + 1 == X)
break;
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
xpos++;
break;
}
}
Console.SetCursorPosition(xpos, ypos);
if (pick == true)
{
Area[xpos, ypos] = Area2[xpos, ypos];
Console.Write(Symbol);
Console.SetCursorPosition(xpos, ypos);
}
}
}
static void Main()
{
Console.Clear();
Checkers check = new Checkers();
check.Checkerboard();
check.Checkerboard2();
Console.SetCursorPosition(0, 0);
check.Keys();
}
}
}
Loading

VulpesRank #55 · 29.2K reputationPosted Apr 25, 2011, 5:27 AM EDT
public char[,] MoveRight(out int col, out int row, out bool redraw)
{
if (pick)
{
xpos++;
col = xpos;
row = ypos;
redraw = false;
}
else
{
xpos++;
col = xpos;
row = ypos;
redraw = true;
Area[xpos, ypos] = Symbol;
}
return Area;
}
Also try to use if/else where possible rather than two if's as the compiler can then follow the possible code paths more easily.
The good news is that I think you're starting to get the jist of this :)
Adam131 reputationPosted Mar 28, 2011, 2:18 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Mar 28, 2011, 3:26 AM EDT
Adam131 reputationPosted Mar 28, 2011, 4:01 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Mar 28, 2011, 5:24 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 28, 2011, 5:41 AM EDT
So, it should be:
void Keys()
{
while (true)
{
var ch = Console.ReadKey().Key;
switch (ch)
{
case ConsoleKey.Enter:
if (Area[ypos, xpos] == Symbol && pick == false)
{
pick = true;
}
else if (Area[ypos, xpos] != Symbol && pick == true)
{
Area[ypos, xpos] = Symbol;
pick = false;
}
break;
case ConsoleKey.UpArrow:
{
if (ypos - 1 < 0)
break;
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
ypos--;
break;
}
case ConsoleKey.DownArrow:
{
if (ypos + 1 == Y)
break;
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
ypos++;
break;
}
case ConsoleKey.LeftArrow:
{
if (xpos - 1 < 0)
break;
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
xpos--;
break;
}
case ConsoleKey.RightArrow:
{
if (xpos + 1 == X)
break;
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[ypos, xpos]);
xpos++;
break;
}
}
Console.SetCursorPosition(xpos, ypos);
if (pick == true)
{
Area[ypos, xpos] = Area2[ypos, xpos];
Console.Write(Symbol);
Console.SetCursorPosition(xpos, ypos);
}
}
}
Adam131 reputationPosted Mar 28, 2011, 7:10 AM EDT
Suthish Nair: Ok so whole requirement is Game Checkers in console aplication, later in windows form. I put here each module because I want also do somethink instead copy whole correct code from forum and do nothing. I also want to program but it seems to be to hard for me, its true:D But its my homework to school.
VulpesRank #55 · 29.2K reputationPosted Mar 28, 2011, 7:27 AM EDT
The first dimension of Area (and Area2) represents the row index and the second dimension represents the column index of the character.
Now, Console.SetColumnPosition sets the column position first and the row position second.
Also, when the cursor is moving down, the row index which you've represented by 'y' gets incremented.
when the cursor is moving to the right the column index which you've represented by 'x' gets incremented.
So, Console.SetColumnPosition(x, y) is correct.
However, Area[x,y] is wrong because the column and row dimensions are the other way around. To fix it, you therefore need to use Area[y,x].
Adam131 reputationPosted Mar 28, 2011, 7:38 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 28, 2011, 8:06 AM EDT
When you only had one figurine, it was hidden by the symmetry of the board which, fortunately, was destroyed when you added the second one :)
What gave it away was when the second figurine which you'd placed at C8 started appearing at A6 when the cursor was moved over it - a real headscratcher ;)
Adam131 reputationPosted Mar 28, 2011, 8:22 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 28, 2011, 9:04 AM EDT
Adam131 reputationPosted Mar 28, 2011, 9:11 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 28, 2011, 10:11 AM EDT
OK, let's have another go with this:
Adam131 reputationPosted Mar 28, 2011, 10:47 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 28, 2011, 10:54 AM EDT
I was trying to do it slightly differently to start with and then forgot to remove them.
Adam131 reputationPosted Mar 30, 2011, 8:02 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 30, 2011, 2:35 PM EDT
If you create a public class called CheckerBoard in your dll, then it will need a public property to expose the current state of the board as a rectangular array of characters.
You'll then need a public method (or methods) to process each move and change the state of the board accordingly and a method to clear the board may also be useful.
I think the .exe itself will need to provide an appropriate method to (re)draw the board as this will, of course, be completely different for console or windows forms applications though, frankly, any professional games programmer who found it necessary to redraw the whole board after each move would probably get the sack!
Adam131 reputationPosted Apr 11, 2011, 4:03 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 11, 2011, 7:20 AM EDT
Adam131 reputationPosted Apr 15, 2011, 3:22 AM EDT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Checkerboard
{
public class Check
{
const int X = 8;
const int Y = 8;
int switcher = 0;
int num = 8;
char[,] Area = new char[X, Y];
char Symbol = (char)5;
char Symbol2 = (char)6;
//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");
}
}
Adam131 reputationPosted Apr 15, 2011, 3:27 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 15, 2011, 5:48 AM EDT
else
{
Console.WriteLine();
}
All the above is off the top of my head so don't be surprised if there are bugs.
So your Check class needs to have methods to manipulate the board for each possible move and return the state if it has changed
so that the .exe can redraw the board. The rules will be embedded in the way the methods in the dll work.
Are you getting the idea now?
Adam131 reputationPosted Apr 17, 2011, 3:56 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 17, 2011, 4:18 AM EDT
Adam131 reputationPosted Apr 18, 2011, 11:40 PM EDT
public 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.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);
}
}
}
VulpesRank #55 · 29.2K reputationPosted Apr 19, 2011, 7:26 AM EDT
Adam131 reputationPosted Apr 25, 2011, 3:26 AM EDT
public char[,] MoveRight(out int col, out int row, out bool redraw)
{
if (pick == true)
{
row++;
return Area[row, col];
}
if (pick == false)
{
row++;
Area[row, col] = Symbol;
}
redraw = true;
return Area;
}
public char[,] EnterPressed(out int col, out int row, out bool redraw)
{
if (pick == true && Area[row, col] == Symbol)
{
pick = false;
}
if (pick == false && Area[row, col] != Symbol)
{
Area[row, col] = Symbol;
redraw = true;
pick = true;
}
return Area;
}
There are errors like: - The out parameter 'col' must be assigned to before control leaves the current method
- Use of unassigned out parameter 'col'
- Cannot implicitly convert type 'char' to 'char[*,*]'
Can u please help me understand it? Thanks
VulpesRank #55 · 29.2K reputationPosted Apr 25, 2011, 4:35 AM EDT
So before the method returns, you have to make sure that all these have been assigned to:
row - the row position of the cursor
col - the column position of the cursor
redraw - whether the board needs redrawing or not
You always need to return the Area array (not just an element of it) whether the board needs redrawing or not.
The dll class also needs to keep track of where the cursor should be (use your xpos and ypos variables) and keep these synchronized with what it's passing back to the .exe. So, you should manipulate xpos and ypos first and then assign them to row and col.
Adam131 reputationPosted Apr 25, 2011, 5:16 AM EDT
public char[,] MoveRight(out int col, out int row, out bool redraw)
{
if (pick == true)
{
xpos++;
col = xpos;
row = ypos;
redraw = false;
return ??;
}
if (pick == false)
{
xpos++;
col = xpos;
row = ypos;
redraw = true;
Area[xpos, ypos] = Area[row, col] = Symbol;
return ??;
}
}
Error 1 'ClassLibrary1.Saveboard.MoveRight(out int, out int, out bool)': not all code paths return a value
Adam131 reputationPosted Apr 25, 2011, 6:09 AM EDT
public char[,] EnterPressed(out int col, out int row, out bool redraw)
{
if (pick == true && Area[xpos, ypos] == Symbol)
{
col = xpos;
row = ypos;
redraw = false;
pick = false;
}
else if (pick == false && Area[xpos, ypos] != Symbol)
{
col = xpos;
row = ypos;
Area[xpos, ypos] = Symbol;
redraw = true;
pick = true;
}
return Area;
}
I think there is all what should be and second think when I press right arrow it does nothink just after second click it works. and the limit about border of board should be written in exe or in dll? I mean that the cursor stop on the last square of array. thx
VulpesRank #55 · 29.2K reputationPosted Apr 25, 2011, 6:38 AM EDT
public char[,] EnterPressed(out int col, out int row, out bool redraw)
{
if (pick == true && Area[xpos, ypos] == Symbol)
{
redraw = false;
pick = false;
}
else if (pick == false && Area[xpos, ypos] != Symbol)
{
Area[xpos, ypos] = Symbol;
redraw = true;
pick = true;
}
else
{
redraw = false;
}
col = xpos; // cursor not moving
row = ypos; // ditto
return Area;
}
Checking that the cursor doesn't go off the board, should be done in the dll. If it has done, then you need to move it back by setting row and col to what they were before in the dll. The exe will then place the cursor back in the correct place.
Adam131 reputationPosted Apr 25, 2011, 7:18 AM EDT
because befeore, without dll, where it was bigger I jsut put there break; so how can I use break in if ?? and other problem is when I pull Enter it starts draw symbol to every place I move, before I had second chceckerboard so should I do it the same way or other? and if i shoul use the same way I've just tried to put code below to dll and in Enter method add Area[xpos,ypos] = Area2[xpos,ypos] and it doesnt work. Area2 maybe have to be created first somewhere but I dont know where exactly. thx
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++;
}
}
VulpesRank #55 · 29.2K reputationPosted Apr 25, 2011, 12:34 PM EDT
Adam131 reputationPosted Apr 26, 2011, 2:22 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Apr 26, 2011, 5:22 AM EDT
Adam131 reputationPosted Apr 26, 2011, 6:20 AM EDT
I think problem is somewhere in else condition, and I dont understand if I change order of lines it works differently. Why? the return gives back only Aray.
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;
}
VulpesRank #55 · 29.2K reputationPosted Apr 26, 2011, 12:46 PM EDT
Adam131 reputationPosted Apr 26, 2011, 1:11 PM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 27, 2011, 1:31 AM EDT
I think it would be a good idea if your started a new thread and posted the code you have so far for the dll and exe.
This thread has become so long that it's virtually impossible now for anyone to figure out anything!
Adam131 reputationPosted Apr 27, 2011, 2:00 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 27, 2011, 2:06 AM EDT
So, I wouldn't bother marking anything as a 'correct answer'.