Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <stdexcept>
- using std::cout;
- using std::cin;
- using std::endl;
- using std::string;
- // using std::;
- string player_marker() {
- char player_asgmt = ' ';
- string xo_or_ox = "";
- while (player_asgmt != 'X' && player_asgmt != 'O') {
- cout << "Player 1, do you want to be X or O?\n";
- cin >> player_asgmt;
- if (player_asgmt == 'X' || player_asgmt == 'x') {
- xo_or_ox = "XO";break;
- } else if (player_asgmt == 'O' || player_asgmt == 'o') {
- xo_or_ox = "OX";break;
- } else {
- cout << "Invalid input\n";
- }
- }
- return xo_or_ox;
- }
- string display(string board) {
- return std::string(1,board[1])+"|"+std::string(1,board[2])+"|"+std::string(1,board[3])+"\n-----\n"+std::string(1,board[4])+"|"+std::string(1,board[5])+"|"+std::string(1,board[6])+"\n-----\n"+std::string(1,board[7])+"|"+std::string(1,board[8])+"|"+std::string(1,board[9]);
- }
- void place_marker(string &board,char mark,int space) {
- board[space] = mark;
- cout << display(board) << endl;
- }
- bool win_check(string board,char mark) {
- bool wincheck = ((board[1] == mark && board[2] == mark && board[3] == mark) ||
- (board[4] == mark && board[5] == mark && board[6] == mark) ||
- (board[7] == mark && board[8] == mark && board[9] == mark) ||
- (board[1] == mark && board[4] == mark && board[7] == mark) ||
- (board[2] == mark && board[5] == mark && board[8] == mark) ||
- (board[3] == mark && board[6] == mark && board[9] == mark) ||
- (board[1] == mark && board[5] == mark && board[9] == mark) ||
- (board[3] == mark && board[5] == mark && board[7] == mark));
- return wincheck;
- }
- int who_goes_first() {
- srand(100);
- int first_player = rand() % 2 + 1;
- return first_player;
- }
- bool space_check(int position, string board) {
- return (board[position] == ' ');
- }
- bool full_board(string board) {
- int clear_spaces = 0;
- for (int i{}; i < 9; ++i) {
- if (space_check(i+1, board))
- clear_spaces++;
- }
- return !(clear_spaces >= 1);
- }
- int player_choice(string board1, string board2) {
- int pos = 0;
- while (pos <= 0 || pos >= 10 || !space_check(pos,board2)) {
- cout << "Where would you like to place your marker? Enter a number from the grid below\n" << display(board1) << endl;
- cin >> pos;
- if (pos <= 0 || pos >= 10 || !space_check(pos,board2))
- cout << "Invalid choice" << endl;
- }
- cout << display(board2);
- return pos;
- }
- bool replay() {
- char yn = ' ';
- while (yn != 'Y' && yn != 'y' && yn != 'N' && yn != 'n') {
- cout << "Do you want to play again? Y/N";
- cin >> yn;
- if (yn == 'Y' || yn == 'y')
- return true;
- else if (yn == 'N' || yn == 'n')
- return false;
- }
- }
- void player1s_turn(string board1, string &board2, char mark) {
- int place = player_choice(board1,board2);
- place_marker(board2,mark,place);
- }
- void player2s_turn(string board1,string &board2, char mark) {
- int place = player_choice(board1,board2);
- place_marker(board2,mark,place);
- }
- int main() {
- string board_indexes = "0123456789";
- string xo = player_marker();
- char player1 = xo[0], player2 = xo[1];
- string current_board = " ";
- cout << "Welcome to Tic Tac Toe!" << endl;
- cout << "Player 1 is " << player1 << ", Player 2 is " << player2 << endl;
- int turn = who_goes_first();
- cout << "Player " << turn << " goes first. (picked randomly)" << endl;
- cout << "Ready to play? Y/N" << endl;
- bool gameon;
- char ready = ' ';
- cin >> ready;
- // cout << display(board_indexes) << endl;
- gameon = (ready == 'Y' || ready == 'y');
- while (gameon) {
- cout << display(current_board) << endl;
- if (full_board(current_board)){
- if ((!win_check(current_board,player1) || win_check(current_board,player2))){
- cout << "Tie game!" << display(current_board) << endl;break;
- } else {
- cout << "Player " << turn % 2 + 1 << " wins!" << endl;break;
- }
- }
- if (turn == 1) {
- if (win_check(current_board,player2)) {
- cout << "Player 2 wins!" << endl;
- gameon = false;
- break;
- } else {
- if (!full_board(current_board)) {
- player1s_turn(board_indexes,current_board,player1);
- system("clear");
- cout << display(current_board) << endl;
- turn = 2;
- } else
- cout << "Tie game!" << endl;break;
- }
- }
- if (turn == 2) {
- if (win_check(current_board,player1)) {
- cout << "Player 1 wins!" << endl;
- gameon = false;
- break;
- } else {
- if (!full_board(current_board)) {
- player2s_turn(board_indexes,current_board,player2);
- system("clear");
- cout << display(current_board) << endl;
- turn = 1;
- }
- else
- cout << "Tie game!" << endl;break;
- }
- }
- if (full_board(current_board) || win_check(current_board,player1) || win_check(current_board,player2)) {
- if (!replay())
- cout << "OK" << endl;break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment