NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Продолжи код Крестики-нолики на C++:

#include <iostream>
using namespace std;

char board[3][3] = {'1','2','3','4','5','6','7','8','9'}; // Начальное состояние игрового поля

void draw_board() // Функция для рисования игрового поля
{
    cout << "-------------" << endl;
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            cout << "| " << board[i][j] << " ";
        }
        cout << "|" << endl;
        cout << "-------------" << endl;
    }
}

bool game_over() // Функция проверки на конец игры
{
    // Проверяем, есть ли выигрышные комбинации
    for(int i=0; i<3; i++)
    {
        if(board[i][0] == board[i][1] && board[i][1] == board[i][2])
        {
            return true;
        }
       
        if(board[0][i] == board[1][i] && board[1][i] == board[2][i])
        {
            return true;
        }
    }
   
    if(board[0][0] == board[1][1] && board[1][1] == board[2][2])
    {
        return true;
    }
   
    if(board[0][2] == board[1][1] && board[1][1] == board[2][0])
    {
        return true;
    }
   
    // Проверяем, есть ли свободные клетки
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            if(board[i][j] != 'X' && board[i][j] != 'O')
            {
                return false;
            }
        }
    }
   
    // Если выигрышных комбинаций нет, а свободных клеток нет, значит игра завершена в ничью
    return true;
}

void play_game() // Основная функция игры
{
    draw_board();
   
    while(!game_over())
    {
        int row, col;
        cout << "Ход игрока 1 (X). Введите номер строки и столбца: ";
        cin >> row >> col;
        row--;
        col--;
       
        if(board[row][col] != 'X' && board[row][col] != 'O')
        {
            board[row][col] = 'X';
        }
        else
        {
            cout << "Эта клетка уже занята. Попробуйте еще раз." << endl;
            continue;
        }
       
        draw_board();
       
        if(game_over())
        {
            break;
        }
       
        cout << "Ход игрока 2 (O). Введите номер строки и столбца: ";
        cin >> row >> col;
        row--;
        col--;
       
        if(board[row][col] != 'X' && board[row][col] != 'O')
        {
            board[row][col] = 'O';
        }
        else
        {
            cout << "Эта клетка уже занята. Попробуйте еще раз." << endl;
            continue;
        }
       
        draw_board();
    }
   
    if(game_over())
    {
        cout << "Игра закончена!" << endl;
        cout << "-----------------" << endl;
       
        if(board[0][0] == board[1][1] && board[1][1] == board[2][2])
        {
            if(board[0][0] == 'X')
            {
                cout << "Победил игрок 1 (X)!" << endl;
            }
            else
            {
                cout << "Победил игрок 2 (O)!" << endl;
            }
        }
       
        if(board[0][2] == board[1][1] && board[1][1] == board[2][0])
        {
            if(board[0][2] == 'X')
            {
                cout << "Победил игрок 1 (X)!" << endl;
            }
            else
            {
                cout << "Победил игрок 2 (O)!" << endl;
            }
        }
       
        for(int i=0; i<3; i++)
        {
            if(board[i][0] == board[i][1] && board[i][1] == board[i][2])
            {
                if(board[i][0] == 'X')
                {
                    cout << "Победил игрок 1 (X)!" << endl;
                }
                else
                {
                    cout << "Победил игрок 2 (O)!" << endl;
                }
            }
           
            if(board[2][i] == board[1][i] && board[1][i] == board[0][i])
            {
                if(board[0][i] == 'X')
                {
                    cout << "Победил игрок 1 (X)!" << endl;
                }
                else
                {
                    cout << "Победил игрок 2 (O)!" << endl;
                }
            }
        }
       
        // Если нет выигрышных комбинаций, то игра закончилась в ничью
        if(!game_over())
        {
            cout << "Игра закончилась в ничью!" << endl;
        }
    }
}

int main()
{
    play_game();
   
    return 0;
}

// Задание:
// Добавьте в игру возможность выбора между игрой вдвоем и игрой против компьютера.
// Если выбрана игра против компьютера, то компьютер должен выбирать случайные свободные клетки поля.
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.