Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // kursovayaC++.cpp
- // Zadach C++
- //
- //
- #include <iostream>
- #include <cstdlib>
- #include <algorithm>
- #include <string>
- #include <clocale>
- using namespace std;
- const char X = 'X', O = '0';
- const int fHeight = 15, fWidth = 15;
- int lengthForWin = 5;
- int curLength;
- char field[fHeight][fWidth];
- int threatMatrix[fHeight][fWidth];
- int maxThreat;
- void cleanField() {
- for (int i = 0; i < fHeight; i++) {
- for (int j = 0; j < fWidth; j++) {
- field[i][j] = ' ';
- threatMatrix[i][j] = 0;
- maxThreat = 0;
- }
- }
- }
- void showField() {
- for (int i = 0; i < fHeight; i++) {
- for (int j = 0; j < fWidth; j++)
- cout << field[i][j] << "|";
- cout << endl;
- }
- }
- int checkLine(int x, int y, int modX, int modY, char theCharacter) {
- int modedX = x, modedY = y;
- curLength = (field[x][y] == theCharacter) ? 1 : 0;
- while (modedX >= 0 && modedX < fHeight && modedY >= 0 && modedY < fWidth) {
- modedX += modX;
- modedY += modY;
- if (theCharacter != field[modedX][modedY])
- break;
- curLength++;
- }
- modedX = x;
- modedY = y;
- while (modedX >= 0 && modedX < fHeight && modedY >= 0 && modedY < fWidth) {
- modedX -= modX;
- modedY -= modY;
- if (theCharacter != field[modedX][modedY])
- break;
- curLength++;
- }
- return curLength;
- }
- bool isWinner(int x, int y, char theCharacter) {
- curLength = checkLine(x, y, 1, 0, theCharacter);
- if (curLength >= lengthForWin)
- return true;
- curLength = checkLine(x, y, 1, 1, theCharacter);
- if (curLength >= lengthForWin)
- return true;
- curLength = checkLine(x, y, -1, 1, theCharacter);
- if (curLength >= lengthForWin)
- return true;
- curLength = checkLine(x, y, 0, 1, theCharacter);
- if (curLength >= lengthForWin)
- return true;
- return false;
- }
- void threatMatrixMod(int x, int y, char theCharacter) {
- threatMatrix[x][y] = -1;
- if (theCharacter == X) {
- for (int i = 1; i < lengthForWin; i++) {
- if (threatMatrix[x - i][y - i] != -1)
- threatMatrix[x - i][y - i] = max(threatMatrix[x - i][y - i], checkLine(x - i, y - i, 1, 1, theCharacter));
- if (threatMatrix[x + i][y + i] != -1)
- threatMatrix[x + i][y + i] = max(threatMatrix[x + i][y + i], checkLine(x + i, y + i, 1, 1, theCharacter));
- if (threatMatrix[x][y - i] != -1)
- threatMatrix[x][y - i] = max(threatMatrix[x][y - i], checkLine(x, y - i, 0, 1, theCharacter));
- if (threatMatrix[x][y + i] != -1)
- threatMatrix[x][y + i] = max(threatMatrix[x][y + i], checkLine(x, y + i, 0, 1, theCharacter));
- if (threatMatrix[x + i][y - i] != -1)
- threatMatrix[x + i][y - i] = max(threatMatrix[x + i][y - i], checkLine(x + i, y - i, 1, -1, theCharacter));
- if (threatMatrix[x - i][y + i] != -1)
- threatMatrix[x - i][y + i] = max(threatMatrix[x - i][y + i], checkLine(x - i, y + i, 1, -1, theCharacter));
- if (threatMatrix[x - i][y] != -1)
- threatMatrix[x - i][y] = max(threatMatrix[x - i][y], checkLine(x - i, y, 1, 0, theCharacter));
- if (threatMatrix[x + i][y] != -1)
- threatMatrix[x + i][y] = max(threatMatrix[x + i][y], checkLine(x + i, y, 1, 0, theCharacter));
- }
- }
- maxThreat = 0;
- for (int i = 0; i < fHeight; i++) {
- for (int j = 0; j < fWidth; j++) {
- if (threatMatrix[i][j] > maxThreat)
- maxThreat = threatMatrix[i][j];
- }
- }
- }
- char play(bool ifFirstPlayer) {
- cleanField();
- string inputX, inputY;
- int turn = 1, x = 0, y = 0;
- while (turn < fHeight * fWidth) {
- bool found = false;
- if (turn % 2 == ifFirstPlayer) {
- while (true) {
- cout << "\nEnter the row index: ";
- cin >> inputX;
- x = atoi(inputX.c_str());
- cout << "Enter the column index: ";
- cin >> inputY; cout << "\n";
- y = atoi(inputY.c_str());
- if (field[x][y] == ' ') {
- field[x][y] = X;
- break;
- }
- else {
- cout << "Somethig's wrong: choose a free cell from 0 to " << fHeight - 1 <<".";
- }
- }
- if (isWinner(x, y, X)){
- showField();
- return 'P';
- }
- else
- threatMatrixMod(x, y, X);
- }
- else {
- if (turn == 1) {
- threatMatrix[fHeight / 2 + fHeight % 2][fWidth / 2 + fWidth % 2] = 100;
- maxThreat = 100;
- }
- else {
- for (int i = 0; i < fHeight; i++) {
- if (found == true)
- break;
- for (int j = 0; j < fWidth; j++) {
- if (isWinner(i, j, O)) {
- x = i;
- y = j;
- found = true;
- break;
- }
- }
- }
- }
- for (int i = 0; i < fHeight; i++) {
- if (found == true)
- break;
- for (int j = 0; j < fWidth; j++) {
- if (threatMatrix[i][j] != maxThreat)
- continue;
- x = i;
- y = j;
- found = true;
- break;
- }
- }
- field[x][y] = O;
- showField();
- if (isWinner(x, y, O))
- return 'B';
- else
- threatMatrixMod(x, y, O);
- }
- turn++;
- }
- return 'D';
- }
- int main() {
- char winner;
- int playerWins, botWins;
- playerWins = botWins = 0;
- string ifFirstPlayer, ifPlaying = "yes";
- printf("You play X, b0t plays 0. Your task is to get %d Xes in a row (or in a column, or diagonally), then you win. Let's get started. \n______________________\n", lengthForWin);
- while (ifPlaying == "yes") {
- while (true) {
- cout << "\n - Would you like to be the first player? (yes or no)\n - ";
- cin >> ifFirstPlayer;
- if (ifFirstPlayer == "yes" || ifFirstPlayer == "no")
- break;
- cout << "\nInvalid input. Let's try one more time: \n";
- }
- winner = play((ifFirstPlayer == "yes") ? 1 : 0);
- cout << endl;
- if (winner == 'P') {
- playerWins++;
- cout << "Player wins!";
- }
- else if (winner == 'B') {
- botWins++;
- cout << "Haha, b0t wins";
- }
- else {
- playerWins++;
- botWins++;
- cout << "It's a draw :)";
- }
- cout << "\nGame statistics: \nPlayer " << playerWins << ":" << botWins << " b0t" << endl;
- while (true) {
- cout << "\n - Would you like to play one more time? (yes or no)\n - ";
- cin >> ifPlaying;
- if (ifPlaying == "yes" || ifPlaying == "no")
- break;
- cout << "\nInvalid input. Please try again: \n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement