Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<Windows.h>
- #include<iostream>
- #include<vector>
- struct ttt {
- int board[3][3];
- int X, O;
- bool player1, player2;
- ttt() : player1{}, player2{}, board{}, X(1), O(-1) {}
- void play() {
- int numberPlayers = 2;
- //how many players?
- display();
- int turns = 0;
- bool flip = false;
- while (checkwin() == ' ' && turns < 9) {
- int move = 0;
- if (numberPlayers == 2) {
- while (move == 0) {
- std::cout << "Player " << flip + 1 << " Enter A Move \n";
- scanf_s("%d", &move);
- if (checkmove(move)) {
- updateboard(move, flip + 1);
- display();
- }
- else {
- std::cout << "Incorrect input or square is already occupied Please Try again\n";
- move = 0;
- }
- }
- }
- else if (numberPlayers == 1) {
- //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- }
- flip = !flip;
- turns++;
- }
- if (checkwin() == ' ') {
- std::cout << " ITS A TIE\n";
- }
- else {
- std::cout << checkwin() << " WINS!!\n";
- }
- std::cout << "Game Over\n";
- }
- std::vector<int> copyboard() {
- std::vector<int> cc(9);
- for (int i = 0; i < 9; i++) {
- cc[i] = board[(int)floor(i / 3)][i % 3];
- }
- return cc;
- }
- int aimove() {
- std::vector<int> cc = copyboard();
- std::vector<int> pms = potentialmoves(cc);
- if (pms.size() == 9) { return 5; }
- std::vector<std::vector<int>> scorelist(pms.size());
- int move = 0;
- return move;
- }
- void aimove_recur(std::vector<int> cc, int* move,bool is_max) {
- }
- std::vector<int> potentialmoves(std::vector<int> cc) {
- //basically empty squares
- std::vector<int> v{};
- for (int i = 0; i < 9; i++) {
- if (cc[i] == 0) {
- v.push_back(i);
- }
- }
- return v;
- }
- //int checktwos(std::vector<int> cc,int opponent) {
- // int xscore = 0;
- // int yscore = 0;
- // int dscore = 0;
- // int xdscore = 0;
- // for (int i = 0; i < 3; i++) {
- // for (int j = 0; j < 3; j++) {
- // xscore += cc[ i * 3 + j ];
- // yscore += cc[ j * 3 + i];
- // if (i == j) {
- // dscore += cc[ (j + i) * 2];
- // xdscore += cc[j + i + 2];
- // }
- // }
- // if (xscore == 2 || yscore == 2 || dscore == 2 || xdscore==2) {
- // return 'X';
- // }
- // xscore = 0;
- // yscore = 0;
- // }
- //}
- void updateboard(int move, byte player) {
- board[(int)floor((move - 1) / 3)][(move - 1) % 3] = player == 1 ? X : O;
- }
- bool checkmove(int move) {
- auto b = board[(int)floor((move-1) / 3)][(move - 1) % 3];
- if (b == 0) { return true; }
- return false;
- }
- void display() {
- std::cout << "\n\n\n";
- for (int i = 0; i < 9; i++) {
- auto b = board[(int)floor(i / 3)][i % 3];
- if (b == X) {
- std::cout << "X ";
- }
- else if (b == O) {
- std::cout << "O ";
- }
- else {
- std::cout << i + 1 << " ";
- }
- if (!((i + 1) % 3)) {
- std::cout << '\n';
- }
- }
- std::cout << "\n\n\n";
- }
- void reset() {
- for (int i = 0; i < 3;i++) {
- for (int j = 0; j < 3; j++) {
- board[i][j] = 0;
- }
- }
- }
- char checkwin() {
- int xscore = 0;
- int yscore = 0;
- int dscore = 0;
- int ddscore = 0;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- xscore +=board[i][j];
- yscore += board[j][i];
- if (i == j) {
- dscore += board[i][j];
- if (i == 0) {
- ddscore += board[i + 2][j];
- }
- else if (i == 1) {
- ddscore += board[i][j];
- }
- else if (i == 2) {
- ddscore += board[i][j - 2];
- }
- }
- }
- if (xscore == 3 || yscore==3 || dscore==3 || ddscore == 3) {
- return 'X';
- }
- else if (xscore == -3 || yscore == -3 || dscore == -3 || ddscore == -3) {
- return 'O';
- }
- xscore = 0;
- yscore = 0;
- }
- return ' ';
- }
- };
- int main() {
- ttt a;
- a.reset();
- a.play();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement