Я очень новичок в C++, и я (случайно) решил, что крестики-нолики - это хорошее место для начала моего первого мини-проекта (не так просто, как я думал...).
В настоящее время у меня есть одна проблема, которую я не могу исправить, когда переменная (playerPlays)
печатается где-то, но не где-то еще.
void inputUpdatePlayerBoard() {
if (playerInput[0] == 'A' && playerInput[1] == '1') {
//these two couts are to debug, but playerPlays prints nothing at all...
cout << "bloopoop" << endl;
cout << playerPlays << endl;
if (playerPlays == 'A') {
cout << "playerInputSucceeded" << endl;
}
}
}
Моя цель в этой части кода — проверить, является ли (playerInput)
определенной ячейкой и текущим игроком, а затем обновить доску, чтобы отразить изменения.
Однако в (if nested in the if)
я не могу получить ожидаемое значение для (playerPlays)
, когда я получаю ожидаемое значение где-то еще.
Должен сказать, что печать — это не то, чего я хочу, а всего лишь способ проверить, что там хранится, что, по-видимому, ничего.
Я не ссылаюсь на какие-либо крестики-нолики, сделанные другими, поскольку я хочу исследовать и учиться сам. Ниже прикреплен мой полный код.
Заранее спасибо за помощь!
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <curses.h>
#include <unistd.h>
#include "brains.hpp"
using namespace std;
bool firstPlay = true;
char playerInput[2];
unsigned int microsecond = 1000000;
char playerNow = 'A';
char playerPlays;
char A1 = ' ', A2 = ' ', A3 = ' ', B1 = ' ', B2 = ' ', B3 = ' ', C1 = ' ', C2 = ' ', C3 = ' ';
char playBoard[] = {A1, A2, A3, B1, B2, B3, C1, C2, C3};
void currentPlayer() {
if (playerNow == 'A') {
playerPlays = 'A';
playerNow = 'B';
}
else if (playerNow == 'B') {
playerPlays = 'B';
playerNow = 'A';
}
else {
cout << "bloop currentPlayer failed" << endl;
}
}
void clearScreen()
{
int n;
for (n = 0; n < 10; n++)
printf( "\n\n\n\n\n\n\n\n\n\n" );
}
void gameEnd() {
clearScreen();
cout << "Game has ended. Player has won!" << endl;
}
void inputUpdatePlayerBoard() {
if (playerInput[0] == 'A' && playerInput[1] == '1') {
cout << "bloopoop" << endl;
cout << playerPlays << endl;
if (playerPlays == 'A') {
cout << "playerInputSucceeded" << endl;
}
}
}
void winLogic() {
if (A1 == A2 == A3)
{
gameEnd();
}
else if (B1 == B2 == B3)
{
gameEnd();
}
else if (C1 == C2 == C3)
{
gameEnd();
}
else if (A1 == B1 == C1)
{
gameEnd();
}
else if (A2 == B2 == C2)
{
gameEnd();
}
else if (A3 == B3 == C3)
{
gameEnd();
}
else if (A1 == B2 == C3)
{
gameEnd();
}
else if (A3 == B2 == C1)
{
gameEnd();
}
}
void roundStart () {
clearScreen();
cout << "===================" << endl;
cout << " Tic-Tac-Choop" << endl;
cout << "===================" << endl;
cout << "| | | |" << endl;
cout << "| " << playBoard[0] <<" | " << playBoard[1] << " | " << playBoard[2] << " |" << endl;
cout << "| | | |" << endl;
cout << "|-----|-----|-----|" << endl;
cout << "| | | |" << endl;
cout << "| " << playBoard[3] <<" | " << playBoard[4] << " | " << playBoard[5] << " |" << endl;
cout << "| | | |" << endl;
cout << "|-----|-----|-----|" << endl;
cout << "| | | |" << endl;
cout << "| " << playBoard[6] <<" | " << playBoard[7] << " | " << playBoard[8] << " |" << endl;
cout << "| | | |" << endl;
cout << "===================" << endl << endl;
currentPlayer();
cout << playerPlays << playerNow << endl;
cout << "Player " << playerPlays << "! Please make your move:" << endl;
scanf("%s", playerInput);
inputUpdatePlayerBoard();
winLogic();
cout << playerInput << endl;
cout << playerInput[0] <<endl;
cout << playerInput[1] <<endl;
}
int main() {
if (firstPlay == true) {
cout << "Welcome!" << endl;
usleep(0.5 * microsecond);
cout << "This is Mark's Tic-Tac-Choop!" <<endl;
usleep(0.5 * microsecond);
cout << "Version: 1.0 alpha." << endl;
usleep(0.5 * microsecond);
cout << "To begin, press ENTER." << endl;
getchar();
cout << "ENTER has been successfully pressed!" << endl;
usleep(0.5 * microsecond);
cout << "Round starting in 3..." << usleep(1 * microsecond) << "2..." << usleep(1 * microsecond) << "1..." << endl;
firstPlay = false;
roundStart();
}
else {
cout << "bloop" << endl;
}
return 0;
}
Здесь метрическая тонна кода. Можете ли вы уменьшить его? Нужны ли нам все эти несвязанные операторы печати? Нужны ли нам все эти несвязанные операторы usleep?
Да, извините, я должен был удалить все лишнее, я сделаю это в будущем @BillLynch