Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- char board[10] = {'0','1','2','3','4','5','6','7','8','9'};
- int win = 0;
- int c;
- int choice;
- int turn = 1;
- void display_board(){
- cout << "-------------" << endl;
- cout << "|" << " " << board[1] << " " << "|" << " " << board[2] << " " << "|" << " " << board[3] << " " << "|" << endl;
- cout << "|---|---|---|" << endl;
- cout << "|" << " " << board[4] << " " << "|" << " " << board[5] << " " << "|" << " " << board[6] << " " << "|" << endl;
- cout << "|---|---|---|" << endl;
- cout << "|" << " " << board[7] << " " << "|" << " " << board[8] << " " << "|" << " " << board[9] << " " << "|" << endl;
- cout << "-------------" << endl;
- }
- bool check_winner(){
- for (int i = 1; i <=3; i++){
- if(board[i] == board[i+1] && board[i+1] == board[i+2]) return true;
- }
- for (int i = 1; i <=3; i++){
- if(board[i] == board[i+3] && board[i+3] == board[i+6]) return true;
- }
- if (board[1] == board[5] && board[5] == board[9]) return true;
- else if (board[3] == board[5] && board[5] == board[7]) return true;
- else return false;
- }
- void player_move(){
- cout << "Podaj gdzie chcesz wykonac ruch: ";
- cin >> choice;
- c = check_winner();
- if(board[choice] == 'X' || board[choice] == 'O'){
- cout << "te miejsce jest juz zajete!" << endl;
- cout << "Podaj gdzie chcesz wykonac ruch: ";
- cin >> choice;
- }
- else if (choice >0 && choice < 10/*choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5 || choice == 6 || choice == 7 || choice == 8 || choice == 9*/){
- board[choice] = 'X';
- check_winner();
- display_board();
- }
- else{
- cout << "Podales bledne miejsce" << endl;
- }
- }
- void body(int i){
- board[i] = 'O';
- cout << "AI wybral pole nr: " << i << endl;
- display_board();
- }
- void vertical(){
- int i = 0;
- while(i<9){
- string temp_str_h = "";
- temp_str_h = temp_str_h + board[i];
- temp_str_h = temp_str_h + board[i + 1];
- temp_str_h = temp_str_h + board[i + 2];
- if(temp_str_h == "XX3") {
- body(3);
- }
- else if(temp_str_h == "X2X") {
- body(2);
- }
- else if(temp_str_h == "1XX") {
- body(1);
- }
- else if(temp_str_h == "XX6") {
- body(6);
- }
- else if(temp_str_h == "X5X") {
- body(5);
- }
- else if(temp_str_h == "4XX") {
- body(4);
- }
- else if(temp_str_h == "XX9") {
- body(9);
- }
- else if(temp_str_h == "X8X") {
- body(8);
- }
- else if(temp_str_h == "7XX") {
- body(7);
- }
- i++;
- }
- }
- void horizontal(){
- int i=0;
- while(i<9){
- string temp_str_h = "";
- temp_str_h = temp_str_h + board[i];
- temp_str_h = temp_str_h + board[i + 3];
- temp_str_h = temp_str_h + board[i + 6];
- if(temp_str_h == "XX7") {
- body(7);
- }
- else if(temp_str_h == "X4X") {
- body(4);
- }
- else if(temp_str_h == "1XX") {
- body(1);
- }
- else if(temp_str_h == "XX8") {
- body(8);
- }
- else if(temp_str_h == "X5X") {
- body(5);
- }
- else if(temp_str_h == "2XX") {
- body(2);
- }
- else if(temp_str_h == "XX9") {
- body(9);
- }
- else if(temp_str_h == "X6X") {
- body(6);
- }
- else if(temp_str_h == "3XX") {
- body(3);
- }
- i++;
- }
- }
- void diagonal_rd(){
- string temp_str_h = "";
- temp_str_h = temp_str_h + board[1] + board[5] + board[9];
- if(temp_str_h == "XX9") {
- body(9);
- }
- else if(temp_str_h == "X5X") {
- body(5);
- }
- else if(temp_str_h == "1XX") {
- body(1);
- }
- }
- void diagonal_ld(){
- string temp_str_h = "";
- temp_str_h = temp_str_h + board[3] + board[5] + board[7];
- if(temp_str_h == "XX7") {
- body(7);
- }
- else if(temp_str_h == "X5X") {
- body(5);
- }
- else if(temp_str_h == "3XX") {
- body(3);
- }
- }
- void ai_move(){
- vertical();
- horizontal();
- diagonal_rd();
- diagonal_ld();
- }
- void game(){
- srand( time( NULL ) );
- display_board();
- while(c == false){
- if(check_winner() == true){
- cout << "KONIEC GRY";
- break;
- }
- player_move();
- turn++;
- if(turn == 2){
- int ai_choice = (rand() % 9) + 1;
- while(board[ai_choice - 1] == 'O'){
- if(board[ai_choice - 1] == 'X' || board[ai_choice - 1] == 'O'){
- ai_choice = (rand() % 9) + 1;
- }
- }
- board[ai_choice] = 'O';
- cout << "AI wybral pole nr: " << ai_choice << endl;
- display_board();
- }
- if(turn % 2 == 1){
- ai_move();
- }
- }
- }
- int main(){
- game();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement