Untitled
unknown
plain_text
2 years ago
2.9 kB
4
Indexable
Never
#include <iostream> #include <vector> #include <string> using namespace std; // global parameters const char X = 'x'; const char O = 'o'; const char EMPTY = ' '; const char TIE = 'T';// ничья const char NO_ONE = 'N';// остаются равные шансы на победу #define BLUE "\033[34m" #define RED "\033[31m" //прототипы void instructions(); char askYesNo(string questions); int askNumber(string questions, int high, int low); char humanPiece(); char opponent(char piece); void displayBoard(const vector<char>& board); //vector<type> == array char winner(const vector<char>& board); bool isLegal(const vector<char>& board, int move); int humanMove(const vector<char>& board, char human); int computerMove(const vector<char>& board, char computer); void announceWinner(char winner, char computer, char human); int main() { } void instructions(){ cout << "Welcome to our game, here you can see your board" << endl; cout << " 0 | 1 | 2 " << endl; cout << " 3 | 4 | 5 " << endl; cout << " 6 | 7 | 8 " << endl; } char askYesNo(string questions){ char response; do{ cout << questions << " (y/n)? "; cin >> response; } while (response != 'y' && response != 'n'); return response; } int askNumber(string questions, int high, int low){ int number; do{ cout << questions << " 0<=number<=8"; cin >> number; } while (number > high || number < low); return number; } char humanPiece() { char responce = askYesNo("Do you want to make first move? "); if (responce == 'y') return X; return O; } char opponent(char piece){ if (piece == X) return O; return X; } void displayBoard(const vector<char>& board){ cout << board[0] << " | " << board[1] << " | " << board[2] << " | " << endl; cout << board[2] << " | " << board[4] << " | " << board[5] << " | " << endl; cout << board[6] << " | " << board[7] << " | " << board[8] << " | " << endl; } char winner(const vector<char>& board){ const int WINNING_COMB[8][3] = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6} }; for (int row = 0; row < 9; row++){ if((board[WINNING_COMB[row][0]] != EMPTY) && (board[WINNING_COMB[row][0]] == board[WINNING_COMB[row][1]]) && (board[WINNING_COMB[row][1]] == board[WINNING_COMB[row][2]])) return board[WINNING_COMB[row][0]]; } for (int i = 0; i < 9; i++){ if (board[i] == EMPTY) return NO_ONE; } return TIE; } bool isLegal(const vector<char>& board, int move){ return (board[move] == EMPTY); }