Array Game
Aug 3, 2015 at 4:44pm UTC
I did post my code earlier. But this time, im stucked at doing X O X O.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h> //switch
#include <fstream> //input output text file
using namespace std;
void new_board();
void rules();
void menu();
int main()
{
char input;
int answer;
cout << endl;
menu();
/*int choices = 0;
choices = menu();
switch(choices)
{
case 1:
{
system("cls");
new_board();
break;
}
case 2:
{
system("cls");
rules();
break;
}
case 3:
{
system("cls");
cout <<"You've chose to quit the game. See you later!" << endl;
exit(1);
}
default:
{
system("cls");
cout <<"Invalid selection"<< endl;
exit(1);
}
}*/
cin >> answer;
if (answer == 1) // new game
{
system("cls" );
new_board();
}
else if (answer == 2) // rules
{
system("cls" );
rules();
system("pause" );
main(); //start back from int main()
cin >> answer;
}
else if (answer == 3) // exit
{
cout << endl;
cout <<"You've chose to exit. See you later!" << endl;
exit(1);
}
else
{
cout << endl;
cout<<"Wrong Selection. Program ends." << endl;
}
return 0;
}
void menu()
{
system("cls" );
cout <<"----------------------------\n" ;
cout <<" Welcome to Othello game!\n" ;
cout <<"----------------------------\n" ;
cout <<" 1. New Game \n" ;
cout <<" 2. Help \n" ;
cout <<" 3. Quit \n" ;
cout <<"----------------------------\n" ;
cout <<"Enter your selection: " ;
}
void new_board()
{
/*char board [ROWS];
cout <<"+-+-+-+-+-+-+-+-+"<< endl;
int count = 16;
for (int k=0; k<=count; k++)
{
cout <<"| ";
count--;
count = 5;
for (int p=0; p<=count; p++)
{
cout <<"| | | | | | | | "<< endl;
cout <<"+-+-+-+-+-+-+-+-+"<< endl;
count--;
}
}
int i;
while(i<ROWS)
{
cout << board[i] <<"|";
i++;
}
cout <<" a b c d e f g h " << endl;*/
int Vertical = 8;
int Horizontal = 8;
char board[Vertical][Horizontal];
for (int row = 0; row<Vertical; row++)
{
for (int col = 0; col <Horizontal; col++)
{
board[col][row] = '.' ; //fill the board with character '.'
}
}
cout <<" +---+---+---+---+---+---+---+---+" << endl;
for (int row = 0; row<Vertical; row++) //'<=' will output 1 extra box, '<' just nice. //build board
{
cout << row+1 <<" | " ;
for (int col = 0; col <Horizontal; col++)
{
cout << board[row][col] <<" | " ;
}
cout << endl;
cout <<" +---+---+---+---+---+---+---+---+" << endl;
}
cout <<" A B C D E F G H" << endl;
}
void rules()
{
cout <<"-------------------------------------------------------------\n" ;
cout <<" Othello Game Rules\n" ;
cout <<"-------------------------------------------------------------\n" ;
cout <<" 1. Black always moves first \n" ;
cout <<" 2. If you cannot outflank at least one opposing disc,\n"
<<" your turn is forfeited and your opponent moves again.\n" ;
cout <<" 3. A disc may outflank any discs in one or more rows\n"
<<" in any directions at the same time.\n" ;
cout <<"-------------------------------------------------------------\n" ;
}
Aug 3, 2015 at 4:46pm UTC
Aug 3, 2015 at 7:48pm UTC
Something like this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
using namespace std;
char board[10][10];
void printBoard()
{
for (int j=0; j<10; j++)
{
for (int i=0; i<10; i++)
{
cout << board[i][j];
}
cout << endl;
}
}
void initializeBoard()
{
for (int j=0; j<10; j++)
{
for (int i=0; i<10; i++)
{
board[i][j] = '.' ;
}
}
}
void inputX()
{
int row = 0;
int col = 0;
cout << "enter row coordinate 0 to 9: " ;
cin >> row;
cout << "enter col coordinate 0 to 9: " ;
cin >> col;
board[row][col]='X' ;
}
int main()
{
initializeBoard();
printBoard();
inputX();
printBoard();
return 0;
}
Last edited on Aug 3, 2015 at 8:06pm UTC
Aug 5, 2015 at 12:29pm UTC
Yes that's correct.. So if i duplicate it and make it to 'O'.. will that restart back the game?
Aug 5, 2015 at 1:51pm UTC
I don't know what you mean by restart the game.
Usually a board game loops until some win/lose/draw state is reached or the user enters some exit value. Maybe something like this,
1 2 3 4 5 6 7 8
int exitValue;
exitValue = testState();
while (exitValue == 0)
{
....
exitValue = testState();
}
Perhaps you should try writing the tic tac toe game first to get the idea as Othello is complex and would require code to flip the tokens after each move if required.
One technique is to use +1 for one player and -1 for the other player.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include <iostream>
using namespace std;
char board[8][8];
int player = 1;
int count = 0;
void printBoard()
{
for (int j=0; j<8; j++)
{
cout << j << " " ;
for (int i=0; i<8; i++) // columns
{
cout << board[i][j] << " " ;
}
cout << endl;
}
cout << " 0 1 2 3 4 5 6 7" << endl;
}
void initializeBoard()
{
for (int j=0; j<8; j++)
{
for (int i=0; i<8; i++)
{
board[i][j] = '.' ;
}
}
}
void makeMove()
{
int row = 0;
int col = 0;
cout << "enter row coordinate 0 to 7: " ;
cin >> row;
cout << "enter col coordinate 0 to 7: " ;
cin >> col;
if (player==1)
{
board[col][row]='X' ;
}
else
{
board[col][row]='O' ;
}
}
int main()
{
initializeBoard();
printBoard();
do
{
makeMove();
printBoard();
player = -player; // swap players
count = count + 1; // count moves
}while (count < 5); // make 5 moves then exit
return 0;
}
Last edited on Aug 5, 2015 at 2:42pm UTC
Aug 5, 2015 at 11:26pm UTC
Here is a program that plays rand tic tac toe games:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
#include <cstdlib>
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
int b[9];
// int result;
int j;
// int gamesCount;
// test for win position
// 3 horizontal wins
int TestWin(int t)
{
int win;
win = 0;
if (b[0]== t && b[1]== t && b[2]== t) {
win = t;
}
if (b[3]== t && b[4]== t && b[5]== t) {
win = t;
}
if (b[6]== t && b[7]== t && b[8]== t) {
win = t;
}
// 3 vertical wins
if (b[0]== t && b[3]== t && b[6]== t) {
win = t;
}
if (b[1]== t && b[4]== t && b[7]== t) {
win = t;
}
if (b[2]== t && b[5]== t && b[8]== t) {
win = t;
}
// cross wins
if (b[0]== t && b[4]== t && b[8]== t) {
win = t;
}
if (b[2]== t && b[4]== t && b[6]== t) {
win = t;
}
return win;
}
void InitializeBoard()
{
for (int j = 0; j<9; j++)
{
b[j] = 0;
}
}
// display board
void DisplayBoard()
{
int k;
k = 0;
cout << "+---+---+---+" << endl;
for (int j = 0; j<3; j++)
{
cout << "|" ;
for (int i = 0; i<3; i++)
{
if (b[k] == 0)
cout << " |" ;
if (b[k] == 1)
cout << " x |" ;
if (b[k] == 2)
cout << " o |" ;
k++;
}
cout << endl;
cout << "+---+---+---+" << endl;
}
cout << endl;
}
int getBoardState()
{
int c = 1;
int n = 0;
for (int j = 0; j<9; j++)
{
if (b[j] == 1)
n = n + c;
if (b[j] == 2)
n = n + c*2;
c = c * 10;
}
return n;
}
void MakeAMove(int t)
{
int choice = 0;
/* initialize random seed: */
srand (time(NULL));
choice = rand()%9; // 0 to 8 returned
while (b[choice]!= 0)
{
choice = rand()%9;
}
b[choice] = t;
}
int PlayAGame()
{
int gamesCount = 0;
int result = 0; // 1 = x win , 2 = y win and 3 = draw
InitializeBoard();
DisplayBoard();
while (result == 0)
{
gamesCount = gamesCount + 1;
MakeAMove(1);
result = TestWin(1);
DisplayBoard();
if (result != 1 && gamesCount < 5)
{
MakeAMove(2);
result = TestWin(2);
DisplayBoard();
}
if (result == 0 && gamesCount == 5)
{
result = 3;
}
cout << getBoardState() << endl;
} // end while
return result;
}
int main(int argc, char *argv[])
{
int result;
char reply;
do
{
for (int z = 0; z < 3; z++)
{
result = PlayAGame();
// print result
if (result == 1)
{
cout << "x won" << endl;
}
else if (result == 2)
{
cout << "o won" << endl;
}
else if (result == 3)
{
cout << "DRAW" << endl;
}
}
cout << "Do you want to continue? y/n " ;
cin >> reply;
}while (reply == 'y' );
return 0;
}
Topic archived. No new replies allowed.