Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by asd ( 17 years ago )
// Praktikum Angewandte Informatik
// Reversi
// Defines
#define SIZE_X 8
#define SIZE_Y 8
#include <iostream>
using namespace std;
void spielfeld_anzeigen(int spielfeld[SIZE_X][SIZE_Y])
{
cout << " ";
for (int j=65;j<65+SIZE_Y;j++)
{
cout << ((char) j) << " " ;
}
cout << endl;
for (int i=0;i<SIZE_X;i++)
{
cout << i+1;
for (int j=0;j<SIZE_Y;j++)
{
switch (spielfeld[j][i])
{
case 0:
cout << " " ;
break;
case 1:
cout << " X";
break;
case 2:
cout << " O";
break;
default:
cout << "Inconsistent data in field!" << endl;
cout << "Aborting .... " << endl;
exit(0);
break;
}
};//for j
cout << endl;
}//for i
}
int gewinner(int spielfeld[SIZE_X][SIZE_Y])
{
int count_p1=0;
int count_p2=0;
for (int i=0;i<SIZE_X; i++)
{
for (int j=0;j<SIZE_Y; j++)
{
//loop through all fields + counting of X (1) and O (2)
if ( spielfeld[SIZE_X][SIZE_Y] == 1 ) //Zählt Pkte von Spieler x (1)
{
count_p1++;
}
if ( spielfeld[SIZE_X][SIZE_Y] == 2 ) //Zählt Pkte von Spieler 0 (2)
{
count_p2++;
}
}
}
if (count_p1 == count_p2)
{
return 0;
}
if (count_p1 > count_p2)
{
return 1;
}
else
{
return 2;
}
}
bool zug_gueltig(int spielfeld[SIZE_X][SIZE_Y], int spieler, int pos_x, int pos_y)
{
// Process all possible directions
int gegner=-(spieler -2) + 1;// the same as: if spieler=1 -> spieler=2 else
// if spieler=2 -> spieler=1
if (spielfeld[pos_x][pos_y]!=0)//check if field is currently empty
{
return false;
}
for (int i=-1;i<=1;i++)
{
for (int j=-1;j<=1;j++)
{
//check if you can find an opponents stone in a neighboring field
//then check if in this direction all stones are opponent stones until
//the line is terminated by one of your own stone
//in that case return true otherwise not
if (spielfeld[pos_x+j][pos_y+i] == gegner) //Prüfen ob gegener auf umliegenden Feld
{
for( int p=pos_y; p <= pos_y; p++) // In Y Richtung gucken
{
}
}
else
{
for ( int k=pos_x; k <= pos_x; k++) // In Y Richtung gucken
{
}
}
}
}
return false;
}
bool zug_ausfuehren(int spielfeld[SIZE_X][SIZE_Y], int spieler, int pos_x, int pos_y)
{
// very similar to function "zug_gueltig" - just take care that all opponent
// stones are changed to yours
}
int moegliche_zuege(int spielfeld[SIZE_X][SIZE_Y], int spieler)
{
}
bool mensch_zug(int spielfeld[SIZE_X][SIZE_Y], int spieler)
{
if (moegliche_zuege(spielfeld,spieler)==0)
{
return false;
}
int px;
int py;
bool repeat=false;
do
{
if (repeat)
{
cout << endl << "Invalid input !" << endl;
}
string eingabe;
eingabe.reserve(50);
cout << endl << "Your move (e.g. A1): " ;
eingabe.erase(eingabe.begin(),eingabe.end());
cin >> eingabe;
px = ((int) eingabe.at(0) ) - 65;
py = ((int) eingabe.at(1) ) - 49;
}
while (repeat=!zug_gueltig(spielfeld,spieler,px,py));
zug_ausfuehren(spielfeld,spieler,px,py);
return true;
}
void Spiel(int spielertyp[2])
{
while (zug_gueltig(spielfeld,spieler,px,py)) //let each player make its moves until no further moves are possible
{
int spielfeld[SIZE_X][SIZE_Y]={
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,1,2,0,0,0},
{0,0,0,2,1,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}};
int aktueller_spieler=1;
aktueller_spieler=-(spieler -2) + 1;
// the same as: if spieler=1 -> spieler=2 else
// if spieler=2 -> spieler=1
bool mensch_zug(spielfeld, aktueller_spieler)
spielfeld_anzeigen(spielfeld);
//let each player make its moves until no further moves are possible
}
switch (gewinner(spielfeld))
{
case 0: cout<<"Unentschieden!"<<endl; break;
case 1: cout<<"Spieler 1 hat gewonnen!"<<endl; break;
case 2: cout<<"Spieler 2 hat gewonnen!"<<endl; break;
}
}
int main(void)
{
int spielfeld[SIZE_X][SIZE_Y]={
{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},{0,0,0,1,2,0,0,0},
{0,0,0,2,1,0,0,0},{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}};
spielfeld_anzeigen(spielfeld);
return 0;
int typ[2] = { 1, 1 };
Spiel(typ);
return 0;
}
Revise this Paste