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
VulpesPosted Apr 27, 2011, 6:06 AM
So, I wouldn't bother marking anything as a 'correct answer'.
AdamPosted Apr 27, 2011, 6:00 AM
VulpesPosted Apr 27, 2011, 5:31 AM
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!
AdamPosted Apr 26, 2011, 5:11 PM
VulpesPosted Apr 26, 2011, 4:46 PM
AdamPosted Apr 26, 2011, 10:20 AM
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;
}
Suthish NairPosted Apr 26, 2011, 9:22 AM
AdamPosted Apr 26, 2011, 6:22 AM
VulpesPosted Apr 25, 2011, 4:34 PM
AdamPosted Apr 25, 2011, 11:18 AM
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++;
}
}
VulpesPosted Apr 25, 2011, 10:38 AM
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.
AdamPosted Apr 25, 2011, 10:09 AM
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
VulpesPosted Apr 25, 2011, 9:27 AM
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 :)
AdamPosted Apr 25, 2011, 9:16 AM
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
VulpesPosted Apr 25, 2011, 8:35 AM
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.
AdamPosted Apr 25, 2011, 7:26 AM
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
VulpesPosted Apr 19, 2011, 11:26 AM
AdamPosted Apr 19, 2011, 3:40 AM
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);
}
}
}
VulpesPosted Apr 17, 2011, 8:18 AM
AdamPosted Apr 17, 2011, 7:56 AM
VulpesPosted Apr 15, 2011, 9:48 AM
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?
AdamPosted Apr 15, 2011, 7:27 AM
AdamPosted Apr 15, 2011, 7:22 AM
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");
}
}
VulpesPosted Apr 11, 2011, 11:20 AM
AdamPosted Apr 11, 2011, 8:03 AM
VulpesPosted Mar 30, 2011, 6:35 PM
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!
AdamPosted Mar 30, 2011, 12:02 PM
VulpesPosted Mar 28, 2011, 2:54 PM
I was trying to do it slightly differently to start with and then forgot to remove them.
AdamPosted Mar 28, 2011, 2:47 PM
VulpesPosted Mar 28, 2011, 2:11 PM
OK, let's have another go with this:
AdamPosted Mar 28, 2011, 1:11 PM
VulpesPosted Mar 28, 2011, 1:04 PM
AdamPosted Mar 28, 2011, 12:22 PM
VulpesPosted Mar 28, 2011, 12:06 PM
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 ;)
AdamPosted Mar 28, 2011, 11:38 AM
VulpesPosted Mar 28, 2011, 11:27 AM
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].
AdamPosted Mar 28, 2011, 11:10 AM
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.
VulpesPosted Mar 28, 2011, 9:41 AM
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);
}
}
}
Suthish NairPosted Mar 28, 2011, 9:24 AM
AdamPosted Mar 28, 2011, 8:01 AM
Suthish NairPosted Mar 28, 2011, 7:26 AM
AdamPosted Mar 28, 2011, 6:18 AM