Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lets play a game of tic-tac-toe
- //Created by Zell Faze
- //Available freely under the CC-0 License
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <stdlib.h>
- using namespace std;
- void outputBoard(int board[][3]);
- void initBoard(int board[][3]);
- int checkWinners(int board[][3]);
- void makeAIMove(int board[][3]);
- int main() {
- int board[3][3];
- string inputStr;
- bool gameDone = false;
- int selectedMove;
- //Initialise the board to empty
- initBoard(board);
- srand(time(NULL));
- //Main game loop
- while (gameDone == false) {
- outputBoard(board);
- bool inputValid = false;
- int x, y;
- do {
- cout << "Where would you like to move? (1-9) ";
- getline(cin, inputStr);
- stringstream(inputStr) >> selectedMove;
- //Lets start assuming the input is valid (kinda odd I know)
- inputValid = true;
- if ((selectedMove < 0) || (selectedMove > 9)) {
- cout << "That move is invalid." << endl;
- inputValid = false;
- }
- x = (selectedMove / 3);
- y = (selectedMove % 3) - 1;
- if (board[x][y] != 0) {
- cout << "That move is invalid." << endl;
- inputValid = false;
- }
- } while (inputValid == false);
- board[x][y] = 1;
- int winner;
- winner = checkWinners(board);
- if (winner == 0) {
- makeAIMove(board);
- }
- winner = checkWinners(board);
- if (winner != 0) {
- gameDone = true;
- outputBoard(board);
- cout << "Player " << winner << " won the game!";
- }
- }
- return 0;
- }
- void initBoard(int board[][3]) {
- for (int x = 0; x < 3; x++) {
- for (int y = 0; y < 3; y++) {
- board[x][y] = 0;
- }
- }
- return;
- }
- void outputBoard(int board[][3]) {
- for (int x = 0; x < 3; x++) {
- for (int y = 0; y < 3; y++) {
- //Output the state
- switch (board[x][y]) {
- case 0:
- cout << " ";
- break;
- case 1:
- cout << "X";
- break;
- case 2:
- cout << "O";
- break;
- }
- //Output the col seperator
- if (y == 2) {
- cout << endl;
- } else {
- cout << "|";
- }
- }
- //Output the row seperator
- if (x != 2) {
- cout << "-----" << endl;
- }
- }
- cout << endl;
- return;
- }
- int checkWinners(int board[][3]) {
- int player = 0;
- //Check for horizontal wins
- for (int x = 0; x < 3; x++) {
- player = board[x][0];
- if (player == 0) {
- continue;
- }
- bool possible = true;
- for (int y = 1; y < 3; y++) {
- if (board[x][y] == player) {
- continue;
- } else {
- possible = false;
- break;
- }
- }
- if (possible == true) {
- return player;
- }
- }
- //Check for vertical wins
- for (int y = 0; y < 3; y++) {
- player = board[0][y];
- if (player == 0) {
- continue;
- }
- bool possible = true;
- for (int x = 1; x < 3; x++) {
- if (board[x][y] == player) {
- continue;
- } else {
- possible = false;
- break;
- }
- }
- if (possible == true) {
- return player;
- }
- }
- //Check for diagonal wins
- player = board[0][0];
- if ((board[1][1] == player) && (board[2][2] == player)) {
- return player;
- }
- player = board[0][2];
- if ((board[1][1] == player) && (board[2][0] == player)) {
- return player;
- }
- return 0;
- }
- void makeAIMove(int board[][3]) {
- int x, y;
- bool validMove = false;
- do {
- x = rand() % 3;
- y = rand() % 3;
- if (board[x][y] == 0) {
- validMove = true;
- }
- } while(validMove == false);
- board[x][y] = 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement