Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<iomanip>
- #include <string>
- using namespace std;
- void drawBoard();
- void getSlotChoice(int& slot);
- void playGame(bool f, bool& in);
- #define PLAYER_X 'X';
- #define PLAYER_Y 'O';
- char lastPlayer = PLAYER_Y;
- char currentPlayer = PLAYER_X;
- int slot;
- char board[3][3] = { { '1','2','3'},{'4','5','6'},{'7','8','9'} };
- int main()
- {
- bool flip = true;
- bool ingame = true;
- int turnCount = 0;
- do {
- drawBoard();
- //getSlotChoice(slot);
- playGame(flip,ingame);
- flip = !flip;
- turnCount++;
- } while (ingame && turnCount<9);
- }
- void drawBoard()
- {
- for (int y = 0; y < 3; y++)
- {
- for (int x = 0; x < 3; x++)
- {
- }
- }
- cout << "current board state" << endl;
- cout << " " << endl;
- cout << " " << board[0][0] << "|" << board[0][1] << "|" << board[0][2] << " " << endl;
- cout << " +-+-+-+ " << endl;
- cout << " " << board[1][0] << "|" << board[1][1] << "|" << board[1][2] << " " << endl;
- cout << " +-+-+-+ " << endl;
- cout << " " << board[2][0] << "|" << board[2][1] << "|" << board[2][2] << " " << endl;
- cout << " " << endl;
- }
- void getSlotChoice(int& slot)
- {
- cout << " player please choose a slot from 1-9 " << endl;
- cin >> slot;
- while ((slot < 1) || (slot > 9))
- {
- cout << "invalid choice , please choose a slot 1-9 ";
- cin >> slot;
- }
- }
- void playGame(bool f,bool& in)
- {
- getSlotChoice(slot);
- auto Player = f ? currentPlayer : lastPlayer;
- board[(slot - slot % 3) / 3][slot%3-1] = Player;
- int Hcount = 0;
- int Vcount = 0;
- int Lcount = 0;
- int Rcount = 0;
- for (auto y = 0; y < 3; y++) { //this is all untested
- Hcount=0;
- Vcount=0;
- for (auto x = 0; x < 3; x++) {
- if (board[x][y] == Player) { Hcount++; }
- if (board[y][x] == Player) { Vcount++; }
- }
- if(Hcount==3 || Vcount==3){break;)
- if (board[y][y] == Player) { Lcount++; }
- if (board[y][2 - y] == Player) { Rcount++; }
- }
- if (Hcount == 3 || Vcount == 3 || Lcount==3 || Rcount==3) {
- cout << "player " << Player << " Wins";
- in = false;
- }
- /* if ((board[0][0] == lastPlayer) && (board[0][1] == lastPlayer) && (board[0][2] == lastPlayer))
- {
- cout << " PLayer Y wins on the top row ! " << endl;
- in = false;
- }
- if ((board[0][0] == currentPlayer) && (board[0][1] == currentPlayer) && (board[0][2] == currentPlayer))
- {
- cout << " player X wins on the top row !" << endl;
- in = false;
- }*/
- }
Add Comment
Please, Sign In to add comment