/*
==Things to fix==
1. As long as a piece has a free move he can jump to any free spot
2. Add piece jumping.
*/
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
enum TEAM { EMPTY, RED, BLACK, FREE };
TEAM gameBoard[8][8];
void makeMove(TEAM);
int checkMove(int, int);
void initBoard(void);
void showBoard(void);
int isEven(int);
int canMove(int, int);
int main() {
TEAM currentTeam = RED;
initBoard();
char c;
do {
switch(currentTeam) {
case RED:
cout << "\n\nRED make your move";
break;
case BLACK:
cout << "\n\nBLACK make your move";
break;
default:
cout << "\n\nWierd error concerning currentTeam\n";
exit(1);
break;
};
showBoard();
cout.lock();
do {
cout << "\n(m)ove, (e)xit: ";
cin >> c;
} while(c!=''m'' && c!=''e'');
cout.unlock();\
if(c==''m'') {
makeMove(currentTeam);
if(currentTeam==BLACK) currentTeam = RED;
else currentTeam = BLACK;
} else {
cout << "\nThanks For Playing!\n(press any key to end)\n";
cout.flush();
while(!getch());
exit(0);
};
} while(1);
return 0;
};
void makeMove(TEAM ct) {
int x, y, dx, dy;
do {
cout << "\nEnter x,y coords of piece to move (i.e. x,y ): ";
cout.flush();
cin >> x;
while(cin.peek()<48 || cin.peek()>57) cin.ignore(1);
cin >> y;
} while(gameBoard[x][y]!=ct || canMove(x,y)!=1);
do {
cout << "\nEnter x,y coords of destination (i.e. x,y ): ";
cout.flush();
cin >> dx;
while(cin.peek()<48 || cin.peek()>57) cin.ignore(1);
cin >> dy;
} while(checkMove(dx,dy)!=1);
switch(ct) {
case RED:
cout << "\nRED wants (" << x << ", " << y << ") -> (" << dx << ", " << dy << ")";
gameBoard[x][y] = FREE;
gameBoard[dx][dy] = RED;
break;
case BLACK:
cout << "\nBLACK wants (" << x << ", " << y << ") -> (" << dx << ", " << dy << ")";
gameBoard[x][y] = FREE;
gameBoard[dx][dy] = BLACK;
break;
default:
cout << "\nReally wierd error where the current Team wasn''t passed properly to the function makeMove(TEAM)";
cout << "\n(press any key to exit)";
cout.flush();
while(!getch());
exit(1);
break;
};
};
int checkMove(int x, int y) {
// check to make sure destination square is open
if(gameBoard[x][y]!=FREE) return 0;
else return 1;
};
int canMove(int x, int y) {
int result = 0;
if(y-1>=0 && x-1>=0) if(gameBoard[x-1][y-1]==FREE) result = 1;
if(y-1>=0 && x+1<8) if(gameBoard[x+1][y-1]==FREE) result = 1;
if(y+1<8 && x-1>=0) if(gameBoard[x][y-1]==FREE) result = 1;
if(y+1<8 && x+1<8) if(gameBoard[x][y+1]==FREE) result = 1;
if(result == 0) cout << "\nCannot move that piece";
return result;
};
void initBoard() {
int x, y;
// First, Initialize everything to EMPTY
for(x=0;x<8;x++) {
for(y=0;y<8;y++) {
gameBoard[x][y] = EMPTY;
};
};
// Mark all moveable squares as FREE
for(x=0;x<8;x++) {
for(y=0;y<8;y++) {
if(isEven(y)==1) {
gameBoard[x*2][y] = FREE;
} else {
gameBoard[(x*2)+1][y] = FREE;
};
};
};
// Set Top 3 Rows to RED TEAM
for(x=0;x<4;x++) {
for(y=0;y<3;y++) {
if(isEven(y)==1) {
gameBoard[x*2][y] = RED;
} else {
gameBoard[(x*2)+1][y] = RED;
};
};
};
// Set Bottom 3 Rows to BLACK TEAM
for(x=0;x<4;x++) {
for(y=5;y<8;y++) {
if(isEven(y)==1) {
gameBoard[x*2][y] = BLACK;
} else {
gameBoard[(x*2)+1][y] = BLACK;
};
};
};
};
void showBoard() {
int x, y;
cout << "\n\n 0 1 2 3 4 5 6 7\n==================\n";
for(y=0;y<8;y++) {
cout << y << "| ";
for(x=0;x<8;x++) {
switch(gameBoard[x][y]) {
case EMPTY:
cout << "- ";
break;
case FREE:
cout << "x ";
break;
default:
cout << gameBoard[x][y] << " ";
break;
};
};
cout << endl;
cout.flush();
};
};
inline int isEven(int x) {
if((x%2) == 0) return 1;
else return 0;
};
Thanks in advance for any help.
40
www.databyss.com
www.omlettesoft.com
"Don''t meddle in the affairs of wizards, for they are subtle and quick to anger."
Error From Nowhere
I wrote this program last night.. and it worked, but i used two different arrays (for some other reason) I realized that the other array was no good so i removed it, and all it''s underlying code. I compile (using MSVC++ 6.0) and no errors... i build exe and no problems... i execute and right after it calls showBoard() from the main method it crashes and i get this error:
Loaded ''C:\WINDOWS\SYSTEM\KERNEL32.DLL'', no matching symbolic information found.
First-chance exception in checkers.exe: 0xC0000005: Access Violation.
The program ''D:\PROGRAMMING\checkers\Debug\checkers.exe'' has exited with code 0 (0x0).
than VC++ 6.0 opens up istream.cpp and points the the first line (which is the lock() function) in the function "int istream::ipfx(int need)"
can anybody help me in my time of need?
-40
Your initBoard() Function doesn't function correctly. The 2nd loop overwrites memory.
// Mark all moveable squares as FREE
for(x=0;x<8;x++) {
for(y=0;y<8;y++) {
if(isEven(y)==1) {
gameBoard[x*2][y] = FREE;
} else {
gameBoard[(x*2)+1][y] = FREE;
};
};
x should only loop to 4 as the other 2 loops do.
===========================
No sense being pessimistic. It wouldn't work anyway.
Edited by - Gav on July 22, 2000 11:11:21 AM
// Mark all moveable squares as FREE
for(x=0;x<8;x++) {
for(y=0;y<8;y++) {
if(isEven(y)==1) {
gameBoard[x*2][y] = FREE;
} else {
gameBoard[(x*2)+1][y] = FREE;
};
};
x should only loop to 4 as the other 2 loops do.
===========================
No sense being pessimistic. It wouldn't work anyway.
Edited by - Gav on July 22, 2000 11:11:21 AM
===========================There are 10 types of people in the world. Those that understand binary and those that don't.( My views in no way reflect the views of my employer. )
God bless you kind sir.... you have saved me!
40
www.databyss.com
www.omlettesoft.com
"Don''t meddle in the affairs of wizards, for they are subtle and quick to anger."
40
www.databyss.com
www.omlettesoft.com
"Don''t meddle in the affairs of wizards, for they are subtle and quick to anger."
-40
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement