I have char[,] array Checkerboard full with symbols. Then I have done cursor movement through arrow keys. And now what I need to do is that when I pull Enter the currenct symbol save. And when now I start move with cursor, the current symbol will move also. It will be "save" in cursor. And when I pull Enter for the second time, it will save to current area when the cursor already is. Hope you understead what I want advice for. below my implemented code. Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Checkers
{
class Checkers
{
public const int X = 8;
public const int Y = 8;
int switcher = 0;
int num = 8;
char i;
public int xpos = 0;
public int ypos = 0;
public char[,] Area = new char[X, Y];
void Checkerboard()
{
Cursor();
for (int i = 0; i < X; i++)
{
for (int y = 0; y < Y; y++)
{
if (Area[i, y] == Area[0, 0])
Area[i, y] = (char)5;
else if (switcher % 2 == 0)
Area[i, y] = 'X';
else
Area[i, y] = 'Y';
switcher++;
Console.Write(Area[i, y]);
}
Console.WriteLine(num);
num--;
switcher++;
}
Console.WriteLine("ABCDEFGH");
}
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:
break;
case ConsoleKey.UpArrow:
{
if (ypos - 1 < 0)
{
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
break;
}
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
ypos--;
break;
}
case ConsoleKey.DownArrow:
{
if (ypos + 1 == Y)
{
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
break;
}
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
ypos++;
break;
}
case ConsoleKey.LeftArrow:
{
if (xpos - 1 < 0)
{
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
break;
}
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
xpos--;
break;
}
case ConsoleKey.RightArrow:
{
if (xpos + 1 == X)
{
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
break;
}
Console.SetCursorPosition(xpos, ypos);
Console.Write(Area[xpos, ypos]);
xpos++;
break;
}
}
Console.SetCursorPosition(xpos, ypos);
}
}
static void Main()
{
Checkers check = new Checkers();
check.Checkerboard();
Console.SetCursorPosition(0, 0);
check.Keys();
Console.ReadLine();
}
}
}
Loading
VulpesPosted Mar 27, 2011, 12:32 PM
1. An array which stores the original state of the board with an 'X' at position A8
2. Another array which initially is the same as #1 except it has the figurine at position A8.
The array in #2 stores the current state of the board and so when you choose a figurine you should reset its position to the corresponding symbol on the original board.
AdamPosted Mar 27, 2011, 1:32 PM
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 currentSymbol = (char)5;
void Checkerboard()
{
Cursor();
for (int i = 0; i < X; i++)
{
for (int y = 0; y < Y; y++)
{
if (Area[i, y] == Area[0, 0])
Area[i, y] = (char)5;
else if (switcher % 2 == 0)
Area[i, y] = 'X';
else
Area[i, y] = 'Y';
switcher++;
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[xpos, ypos] == currentSymbol && pick == false)
{
pick = true;
}
if (Area[xpos, ypos] != currentSymbol && pick == true)
{
Area[xpos, ypos] = currentSymbol;
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(currentSymbol);
}
Console.SetCursorPosition(xpos, ypos);
}
}
static void Main()
{
Console.Clear();
Checkers check = new Checkers();
check.Checkerboard();
check.Checkerboard2();
Console.SetCursorPosition(0, 0);
check.Keys();
}
}
}
Suthish NairPosted Mar 27, 2011, 1:23 PM
VulpesPosted Mar 27, 2011, 1:02 PM
Suthish NairPosted Mar 27, 2011, 12:49 PM
Suthish NairPosted Mar 27, 2011, 12:48 PM
using System;
AdamPosted Mar 27, 2011, 12:19 PM
Suthish NairPosted Mar 27, 2011, 12:18 PM
ha ha great project for learning, am thrilled with this thread. Great samples also from Vulpes..
My little contribution also...
try this code, am not sure this will help you..
but the good part is the symbol getting saved and changing its position also..
VulpesPosted Mar 27, 2011, 12:04 PM
So, if you press enter on the 'clubs' figurine (in position A8), and then move the cursor to position A7, then the symbol at that location should permanently change from a 'Y' to the figurine?
AdamPosted Mar 27, 2011, 9:32 AM
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 currentSymbol = (char)5;
void Checkerboard()
{
Cursor();
for (int i = 0; i < X; i++)
{
for (int y = 0; y < Y; y++)
{
if (Area[i, y] == Area[0, 0])
Area[i, y] = (char)5;
else if (switcher % 2 == 0)
Area[i, y] = 'X';
else
Area[i, y] = 'Y';
switcher++;
Console.Write(Area[i, y]);
}
Console.WriteLine(num);
num--;
switcher++;
}
Console.WriteLine("ABCDEFGH");
}
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] == currentSymbol && pick == false)
pick = true;
if (Area[xpos, ypos] != currentSymbol && pick == true)
{
Area[xpos, ypos] = currentSymbol;
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)
Console.Write(currentSymbol);
Console.SetCursorPosition(xpos, ypos);
}
}
static void Main()
{
Console.Clear();
Checkers check = new Checkers();
check.Checkerboard();
Console.SetCursorPosition(0, 0);
check.Keys();
Console.ReadLine();
}
}
}
Enter is for choose the figurine it works. After you choose figurine you move with cursor and it move in cursor also so its ok. But what I want is that when I choose figurine and move with it to other position and save it on new position, the figurine is still on previous position. So I move with it but also duplicate it. Solution like: Area[0,0] = 'X' doesnt help me because later there will be about 24 figurines and they could be on any position during game.
VulpesPosted Mar 26, 2011, 6:52 PM
AdamPosted Mar 26, 2011, 6:24 PM
VulpesPosted Mar 26, 2011, 6:06 PM
If you make the changes which I've highlighted, then the symbol (i.e. the 'clubs' symbol) should move as the cursor moves and, when you press enter, it will be saved at that position.
Notice that I've had to comment out some lines to get it to work: