Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "default.h"
- #include <ctype.h>
- using std::stringstream;
- void DrawLine (string Char, int Len = 80)
- {
- int x = 0;
- cout << endl;
- for (int i = 0;i < Len ;i++)
- {
- cout << Char[x];
- x++;
- if (x == Char.size() )
- x = 0;
- }
- if (Len < 80)
- cout << endl;
- }
- void Swap (int &a, int &b)
- {
- a = a+b;
- b = a-b;
- a = a-b;
- }
- enum User_t { First, Second};
- class Board_t
- {
- private:
- char *VirtBoard;
- public:
- Board_t ();
- char board[9];
- void PrintBoard();
- void SetBoard();
- void UpdateBoard();
- void Move();
- ~Board_t ()
- {
- delete [] VirtBoard;
- }
- };
- Board_t BRD;
- Board_t *Board = &BRD;
- Board_t::Board_t ()
- {
- VirtBoard = new char[9];
- *VirtBoard = *board;
- }
- void Board_t::UpdateBoard ()
- {
- *VirtBoard = *board;
- }
- void Board_t::SetBoard()
- {
- for (int i = 0; i < 9; i++)
- {
- board[i] = ' ';
- }
- }
- void Board_t::PrintBoard()
- {
- cout << endl;
- int x = 0;
- cout << "\t A B C\n";
- for (int i = 0; i < 3; i++)
- {
- cout << "\t" << i+1 << " ";
- for (int j = 0; j < 3; j++)
- {
- cout << board[x];
- if (j != 2)
- cout << "|";
- x++;
- }
- if ( i != 2)
- {
- cout << "\n\t -|-|-";
- }
- cout << endl;
- }
- cout << endl;
- }
- class Peg_t
- {
- public:
- char Peg;
- string UserName;
- User_t UserNum;
- bool VictoryStat();
- Peg_t(char, User_t);
- };
- Peg_t::Peg_t(char A, User_t B)
- {
- Peg = A;
- UserNum = B;
- }
- bool Peg_t::VictoryStat()
- {
- if
- (
- (
- ((Board -> board[0] == Peg) && (Board -> board[1] == Peg) && (Board -> board[2] == Peg)) ||
- ((Board -> board[3] == Peg) && (Board -> board[4] == Peg) && (Board -> board[5] == Peg)) ||
- ((Board -> board[6] == Peg) && (Board -> board[7] == Peg) && (Board -> board[8] == Peg))
- )||
- (
- ((Board -> board[0] == Peg) && (Board -> board[3] == Peg) && (Board -> board[6] == Peg)) ||
- ((Board -> board[1] == Peg) && (Board -> board[4] == Peg) && (Board -> board[7] == Peg)) ||
- ((Board -> board[2] == Peg) && (Board -> board[5] == Peg) && (Board -> board[8] == Peg))
- )||
- (
- ((Board -> board[0] == Peg) && (Board -> board[4] == Peg) && (Board -> board[8] == Peg)) ||
- ((Board -> board[2] == Peg) && (Board -> board[4] == Peg) && (Board -> board[6] == Peg))
- )
- )
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- int main ()
- {
- int a = 0, b = 1;
- string Move;
- stringstream Col, Row;
- char col;
- int row;
- char Peg;
- Peg_t User1 ('X', First);
- Peg_t User2 ('O', Second);
- cout << "Name of Player 1: ";
- getline (cin, User1.UserName);
- cout << "Name of Player 2: ";
- getline (cin, User2.UserName);
- Board -> SetBoard();
- for (int i = 0; i < 9; i++)
- {
- Board -> PrintBoard();
- if (User1.VictoryStat())
- {
- cout << "\n\nCongratulations " << User1.UserName << " You have won the Game.";
- break;
- }
- else if (User2.VictoryStat())
- {
- cout << "\n\nCongratulations " << User2.UserName << " You have won the Game.";
- break;
- }
- cout << "Please enter your move Player ";
- if (i%2 == 0)
- {
- cout << User1.UserNum+1;
- }
- else
- {
- cout << User2.UserNum+1;
- }
- cout << ": ";
- while (true)
- {
- getline (cin, Move);
- if (Move.size() == 2)
- break;
- }
- Col << Move[0];
- Row << Move[1];
- col = Col.str()[0];
- row = atoi((Row.str()).c_str());
- col = toupper (col);
- if (a == 0)
- Peg = User1.Peg;
- else if (a == 1)
- Peg = User2.Peg;
- if (col == 'A')
- {
- if (row == 1 && Board ->board[0] == ' ')
- {
- Board ->board[0] = Peg;
- }
- else if (row == 2 && Board ->board[3] == ' ')
- {
- Board ->board[3] = Peg;
- }
- else if (row == 3 && Board ->board[6] == ' ')
- {
- Board ->board[6] = Peg;
- }
- }
- if (col == 'B')
- {
- if (row == 1 && Board ->board[1] == ' ')
- {
- Board ->board[1] = Peg;
- }
- else if (row == 2 && Board ->board[4] == ' ')
- {
- Board ->board[4] = Peg;
- }
- else if (row == 3 && Board ->board[7] == ' ')
- {
- Board ->board[7] = Peg;
- }
- }
- if (col == 'C')
- {
- if (row == 1 && Board ->board[2] == ' ')
- {
- Board ->board[2] = Peg;
- }
- else if (row == 2 && Board ->board[5] == ' ')
- {
- Board ->board[5] = Peg;
- }
- else if (row == 3 && Board ->board[8] == ' ')
- {
- Board ->board[8] = Peg;
- }
- }
- DrawLine ("-");
- Board -> UpdateBoard();
- Col.str("");
- Row.str("");
- Swap (a,b);
- }
- ;cout << "\nPress ENTER to exit . . .";
- cin.sync();
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment