问题描述
我正在尝试创建一个游戏(井字游戏),我必须使用一个库。我使用 ncurses 使外观更具吸引力。但是,每次我在我的代码中实现 ncurses 时,它都会运行,但不会运行我启动游戏的主代码。它要么退出,要么不允许我看到用户输入。我该怎么办?这是代码:
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include "board.h"
#include "players.h"
#include <ncurses.h>
using namespace std;
/**
* Controls main function of the game
*/
int main(int argc,char** argv) {
initscr();
cbreak();
refresh();
WINDOW* windowTest = newwin(1,23,3,0);
wprintw(windowTest,"Hello World");
//refresh();
wrefresh(windowTest);
getch();
delwin(windowTest);
endwin();
return 0;
string name;
cout << "What's your name: ";
cin >> name;
cout << "Nice to meet you,"<< name << ". Welcome to Tic Tac Toe!" << endl;
//Player is a object and does not matter here
Player game;
game.play();
return 0;
};
解决方法
问题在于 IPAddress ip = IPAddress.Parse("192.168.1.2");
int port = 5555;
TcpClient client = new TcpClient();
client.Connect(ip,port);
NetworkStream networkStream = client.GetStream();
BinaryWriter sendUsername = new BinaryWriter(networkStream);
sendUsername.Write(Environment.UserName);
while (true)
{
NetworkStream stream = client.GetStream();
BinaryReader reader = new BinaryReader(stream);
string messageFromServer = reader.ReadString();
Console.WriteLine(messageFromServer);
}
函数中间的这三个语句,在您创建 main
对象之前:
game
delwin(windowTest);
endwin();
return 0;
和 delwin
调用应该在 endwin
调用之后。并且应该删除此位置的 game.play()
语句。
看来你拿了一些“测试”程序,只是在return
函数的末尾加了几条语句。
您需要更好地规划您的程序及其工作方式。最好从需求列表开始,根据这些需求进行分析,然后根据分析创建一个设计,然后实施。
虽然测试程序非常有用,但当您开始实际项目时,我建议您从头开始。那么这样的错误不应该发生。