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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
void set_board (string **p_p_omok_board, int board_size)
{
int seq_number = 0;
for (int i = 0; i < board_size; i++)
{
for (int j = 0; j < board_size; j++)
{
ostringstream convert;
convert << seq_number;
p_p_omok_board[i][j] = convert.str();
seq_number++;
}
}
}
void set_check_win (int **p_p_check_win, int board_size)
{
for (int i = 0; i < board_size; i++)
{
for (int j = 0; j < board_size; j++)
{
p_p_check_win[i][j] = 2;
}
}
}
void find_value (int &row_found, int &col_found, string player_input, string **p_p_omok_board, int board_size)
{
row_found = -1;
col_found = -1;
for (int row = 0; row < board_size; row++)
{
for (int col = 0; col < board_size; col++)
{
if (p_p_omok_board[row][col] == player_input)
{
row_found = row;
col_found = col;
return;
}
}
}
}
void print_board (string **p_p_omok_board, int board_size)
{
if (board_size <= 10)
{
cout << endl;
for (int i = 0; i < board_size; i++)
{
for (int j = 0; j < board_size; j++)
{
cout << "|";
cout.width(2);
cout << left << p_p_omok_board[i][j];
}
cout << "|" << endl;
cout << "-";
for (int j = 0; j < board_size; j++)
{
cout << "---";
}
cout << endl;
}
}
if (board_size > 10)
{
cout << endl;
for (int i = 0; i < board_size; i++)
{
for (int j = 0; j < board_size; j++)
{
cout << "|";
cout.width(3);
cout << left << p_p_omok_board[i][j];
}
cout << "|" << endl;
cout << "-";
for (int j = 0; j < board_size; j++)
{
cout << "----";
}
cout << endl;
}
}
}
void free_board (string **p_p_omok_board, int board_size)
{
for (int i = 0; i < board_size; i++)
{
delete [] p_p_omok_board[i];
}
delete [] p_p_omok_board;
}
int main ()
{
int board_size = 0;
cout << "Enter board size: ";
cin >> board_size;
int max_num_of_moves = board_size * board_size;
int player_one_move = 0;
string player_one_input;
int player_two_move = 1;
string player_two_input;
string **p_p_omok_board;
p_p_omok_board = new string*[board_size];
for (int i = 0; i < board_size; i++)
{
p_p_omok_board[i] = new string[board_size];
}
int **p_p_check_win;
p_p_check_win = new int*[board_size];
for (int i = 0; i < board_size; i++)
{
p_p_check_win[i] = new int[board_size];
}
set_check_win (p_p_check_win, board_size); //sets all the elements in the array to 2.
set_board (p_p_omok_board, board_size);
print_board (p_p_omok_board, board_size);
for (int i = 0; i < max_num_of_moves; i++)
{
if (player_one_move == i)
{
while (cin >> player_one_input)
{
int row_found = -1;
int col_found = -1;
find_value (row_found, col_found, player_one_input, p_p_omok_board, board_size);
if (p_p_check_win[row_found][col_found] == 0 || p_p_check_win[row_found][col_found] == 1)
{
cout << "Invalid move!" << endl;
}
else
{
break;
}
}
for (int row = 0; row < board_size; row++)
{
for (int col = 0; col < board_size; col++)
{
if (p_p_omok_board[row][col] == player_one_input)
{
p_p_omok_board[row][col] = "o";
p_p_check_win[row][col] = 0;
}
}
}
player_one_move = player_one_move + 2;
}
else if (player_two_move == i)
{
cin >> player_two_input;
for (int row = 0; row < board_size; row++)
{
for (int col = 0; col < board_size; col++)
{
if (p_p_omok_board[row][col] == player_two_input)
{
p_p_omok_board[row][col] = "x";
p_p_check_win[row][col] = 1;
}
}
}
player_two_move = player_two_move + 2;
}
print_board (p_p_omok_board, board_size);
}
free_board (p_p_omok_board, board_size);
return 0;
}
|