Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // TicTacToe
- //
- // Created by Aldís Eik Armarsdóttir on 10/12/17.
- // Copyright © 2017 Aldís Eik Armarsdóttir . All rights reserved.
- //
- #include <iostream>
- using namespace std;
- const int MAX_SIZE = 3;
- void print_char (char tictactoe[][MAX_SIZE])
- {
- for(int i = 0; i < MAX_SIZE; i++)
- {
- for(int j = 0; j < MAX_SIZE; j++)
- {
- cout << tictactoe[i][j] << " ";
- }
- cout << endl;
- }
- }
- void game_players(int& position, char& player, char playerX)
- {
- if(player == playerX)
- {
- cout << "X position: ";
- cin >> position;
- }
- else
- {
- cout << "O position: ";
- cin >> position;
- }
- }
- void players_switch(int& position, char& player, char playerX, char playerO)
- {
- if(player == playerX)
- {
- player = playerO;
- }
- else
- {
- player = playerX;
- }
- }
- bool game_playing (char tictactoe[][MAX_SIZE], int& position, char& player, char playerX, char playerO)
- {
- bool positionOK = false;
- do{
- if (position > 9 || position < 1 || position < - 1 || position == playerX || position == playerO)
- {
- cout << "Illegal move!" << endl;
- game_players(position, player, playerX);
- }
- else if (position > 0 && position <= 9)
- {
- positionOK = true;
- tictactoe[0][position - 1] = player;
- }
- }while (!positionOK);
- return position;
- }
- bool lookforwin(char tictactoe[][MAX_SIZE], bool& win, char& player)
- {
- for(int i = 0; i < 3; i++)
- {
- if ((tictactoe[i][0] == tictactoe[i][1] && tictactoe[i][1] == tictactoe[i][2]) || (tictactoe[0][i] == tictactoe[1][i] && tictactoe[1][i] == tictactoe[2][i]) || (tictactoe[0][0] == tictactoe[1][1] && tictactoe[1][1] == tictactoe[2][2]) || (tictactoe[0][2] == tictactoe[1][1] && tictactoe[1][1] == tictactoe[2][0]))
- {
- win = true;
- }
- }
- if (win == true) {
- cout << "Winner is: " << player << endl;
- }
- return win;
- }
- int main()
- {
- char tictactoe[MAX_SIZE][MAX_SIZE] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'} };
- char playerX = 'X', playerO = 'O';
- char player = playerX;
- int position = 0;
- bool win = false;
- int counter = 0;
- print_char(tictactoe);
- do
- {
- if (counter == 9) {
- cout << "It's a draw!!!!!!!!" << endl;
- break;
- }
- game_players(position, player, playerX);
- //errors ef char, eða 'X', eða 'O' virka ekki
- game_playing(tictactoe, position, player, playerX, playerO);
- print_char(tictactoe);
- lookforwin(tictactoe, win, player);
- //lykkjan þarf að stoppa eftir að win eða draw er til staðar + prentar þrisvar þegar skáruna er
- //setja draw in eftir 9 tilraunir
- players_switch(position, player, playerX, playerO);
- counter++;
- }while(!win); //setja draw inní dowhile eftir 9 skipti
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement