Игра в спички - исправить ошибку в коде - C#
Формулировка задачи:
Здравствуйте, помогите исправить ошибку в программе
Вот сама ошибка
Ссылка на исходники
Projects.7z
Код Сервер
Код Клиента
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
class Server
{
TcpListener Listener;
static void Main()
{
new Server(8080); // 8080 - номер порта, можно поменять если этот закрыт
}
public Server(int Port)
{
Listener = new TcpListener(IPAddress.Any, Port);
Listener.Start();
Console.WriteLine("Welcome to Serverside");
while (true)
{
Console.WriteLine("Ожидание подключения... ");
TcpClient client = Listener.AcceptTcpClient();
Console.WriteLine("Клиент подключен");
NetworkStream stream = client.GetStream();
int idata= int.Parse(recv(stream)); //1
int icolpal = int.Parse(recv(stream)); //2
switch (idata)
{
case 1:
Console.WriteLine("Я хочу поиграть с компьютером с " + icolpal + "палками");
games(stream, icolpal);
break;
case 2:
Console.WriteLine("Я хочу создать сетевую игру");
games(stream, icolpal, 2);
break;
case 3:
Console.WriteLine("Я хочу присоеденится к игре ");
games(stream, icolpal, 3);
break;
}
}
}
private void games(NetworkStream stream, int colpal,int cpuu = 1)
{
Random rnd = new Random();
int i; int lost; int other; ;
lost = colpal;
int hod = rnd.Next(1, 2);// 1 клиент 2 комп
Console.WriteLine(hod);
do{
if (hod == 1)
{
send(stream, "1"); hod = 2;
string data = recv(stream); //
string[] split = data.Split(';');//
lost = int.Parse(split[0]);//
other = int.Parse(split[1]);//
}
else
{
send(stream, "2"); hod = 1;
if (cpuu == 1) other = this.cpuu(lost);
else
{
Console.WriteLine("Выберите общее кол-во количество палочек");
string scolpal = Console.ReadLine();// в строку кол-во палок
other = int.Parse(scolpal);// в инт палки
}
send(stream, lost + ";" + other);
}
}while (lost != 0);
}
private int cpuu(int lost)
{
if (lost < 5) return lost;
else return lost % 5;
}
private void send(NetworkStream streaming, string data)
{
byte[] cpuorplayer = System.Text.Encoding.UTF8.GetBytes(data);
streaming.Write(cpuorplayer, 1, cpuorplayer.Length);
}
private string recv(NetworkStream streaming)
{
Byte[] bytes = new Byte[1024];
int i = streaming.Read(bytes, 0, bytes.Length);
string data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
return data;
}
}using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
class SClient
{
//private Byte[] bytes = new Byte[1024];
TcpClient ipClient;
public SClient(int Port, string Host)
{
//создаем клиента и подключаем к серверу
ipClient = new TcpClient();
ipClient.Connect(Host, Port);
string switchplay;
Console.WriteLine("Welcome to Clientside\n");
NetworkStream stream = ipClient.GetStream();// создаем поток
do
{
Console.WriteLine("Выберите с кем вы хотите сыграть\n1.)С копьютером\n2.)С другим игроком(создать)\n3.)С другим игроком(соедениться)");
switchplay = Console.ReadLine(); //
} while (!switchplay.Equals("1") && !switchplay.Equals("2") && !switchplay.Equals("3"));
byte[] cpuorplayer = System.Text.Encoding.UTF8.GetBytes(switchplay); //в байты выбор
stream.Write(cpuorplayer, 0, cpuorplayer.Length);
switch (switchplay)
{
case "1":
game1(stream);
break;
case "2":
game2(stream);
break;
case "3":
game3(stream);
break;
}
}
static void Main()
{
new SClient(8080, "localhost"); //8080 - порт, "localhost" имя компа в сети на котором запущен сервер.
}
private void games(NetworkStream streaming, int colpal)
{
int i; int lost = colpal; int other;
Console.WriteLine("Игра началась с компьютером с " + colpal + " палочками\n" );
string hod = recv(streaming);
do
{
if (hod.Equals("2"))
{
hod = "1";
string data = recv(streaming); //
string[] split = data.Split(';');//
lost = int.Parse(split[0]);//
other = int.Parse(split[1]);//
Console.WriteLine("Компьютер убрал " + other + " палочек\n осталось " + lost + " палочек!\n");//
}else
{
hod = "2";
string select; int intselect;//
do
{
Console.WriteLine(showpal(lost)+"Ведите количество палочек которые вы хотите удалить(от 1 до 4)");
select = Console.ReadLine(); // в строку выбор
intselect = int.Parse(select); // в инт выбор
} while (intselect <= 0 && intselect >= 5 && lost < intselect); //выполнять если не с 1 по 4 и если хотим убрать палок больше чем можно
byte[] bselect = System.Text.Encoding.UTF8.GetBytes(lost+";"+select); //в байты выбор
streaming.Write(bselect, 0, bselect.Length);
lost = lost - intselect;
}
} while (lost!=0); //палки кончились
recv(streaming);// выйграл или проиграл
}
private void game1(NetworkStream streaming)
{
Console.WriteLine("Выберите общее кол-во количество палочек");
string scolpal = Console.ReadLine();// в строку кол-во палок
int colpal = int.Parse(scolpal);// в инт палки
send(streaming, scolpal);// отправить кол палок
games(streaming, colpal);// запустить игру
}
private void game2(NetworkStream streaming)
{
Console.WriteLine("Выберите общее кол-во количество палочек");
string scolpal = Console.ReadLine();// в строку кол-во палок
int colpal = int.Parse(scolpal);// в инт палки
send(streaming, scolpal);// отправить кол палок
games(streaming, colpal);// запустить игру
}
private void game3(NetworkStream streaming)
{
string data = Console.ReadLine();
byte[] cpuorplayer = System.Text.Encoding.UTF8.GetBytes(data);
streaming.Write(cpuorplayer, 0, cpuorplayer.Length);
//games(streaming, colpal);
}
private void send(NetworkStream streaming, string data)
{
byte[] cpuorplayer = System.Text.Encoding.UTF8.GetBytes(data);
streaming.Write(cpuorplayer, 0, cpuorplayer.Length);
}
private string recv(NetworkStream streaming)
{
Byte[] bytes = new Byte[1024];
int i = streaming.Read(bytes, 0, bytes.Length);
string data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
return data;
}
private string showpal(int col){
string show= "";
for(int i=0 ; i<col;i++){
show+="| ";
}
return "";
}
}Решение задачи: «Игра в спички - исправить ошибку в коде»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
class SClient
{
//private Byte[] bytes = new Byte[256];
TcpClient ipClient;
public SClient(int Port, string Host)
{
//создаем клиента и подключаем к серверу
ipClient = new TcpClient();
ipClient.Connect(Host, Port);
string switchplay;
Console.WriteLine("Welcome to Clientside\n");
NetworkStream stream = ipClient.GetStream();// создаем поток
do
{
Console.WriteLine("Выберите с кем вы хотите сыграть\n1.)С копьютером\n2.)С другим игроком(создать)\n3.)С другим игроком(соедениться)");
switchplay = Console.ReadLine(); //
} while (!switchplay.Equals("1") && !switchplay.Equals("2") && !switchplay.Equals("3"));
byte[] cpuorplayer = System.Text.Encoding.UTF8.GetBytes(switchplay); //в байты выбор
stream.Write(cpuorplayer, 0, cpuorplayer.Length);
switch (switchplay)
{
case "1":
game1(stream);
break;
case "2":
game2(stream);
break;
case "3":
game3(stream);
break;
}
}
static void Main()
{
new SClient(8080, "localhost"); //8080 - порт, "localhost" имя компа в сети на котором запущен сервер.
}
private void games(NetworkStream streaming, int colpal)
{
int i; int lost = colpal; int other;
Console.WriteLine("Игра началась с компьютером с " + colpal + " палочками\n" );
string hod = recv(streaming);
do
{
if (hod.Equals("2"))
{
hod = "1";
string data = recv(streaming); //
string[] split = data.Split(';');//
Console.WriteLine(data);
lost = int.Parse(split[0]);//
Console.WriteLine(lost);
other = int.Parse(split[1]);//
Console.WriteLine("Компьютер убрал " + other + " палочек\n осталось " + (lost-other) + " палочек!\n");//
}else
{
hod = "2";
string select; int intselect;//
do
{
Console.WriteLine(showpal(lost)+"Ведите количество палочек которые вы хотите удалить(от 1 до 4)");
select = Console.ReadLine(); // в строку выбор
intselect = int.Parse(select); // в инт выбор
} while (intselect <= 0 && intselect >= 5 && lost < intselect); //выполнять если не с 1 по 4 и если хотим убрать палок больше чем можно
byte[] bselect = System.Text.Encoding.UTF8.GetBytes(lost+";"+select); //в байты выбор
streaming.Write(bselect, 0, bselect.Length);
lost = lost - intselect;
}
} while (lost>0); //палки кончились
recv(streaming);// выйграл или проиграл
}
private void game1(NetworkStream streaming)
{
Console.WriteLine("Выберите общее кол-во количество палочек");
string scolpal = Console.ReadLine();// в строку кол-во палок
int colpal = int.Parse(scolpal);// в инт палки
send(streaming, scolpal);// отправить кол палок
games(streaming, colpal);// запустить игру
}
private void game2(NetworkStream streaming)
{
Console.WriteLine("Выберите общее кол-во количество палочек");
string scolpal = Console.ReadLine();// в строку кол-во палок
int colpal = int.Parse(scolpal);// в инт палки
send(streaming, scolpal);// отправить кол палок
games(streaming, colpal);// запустить игру
}
private void game3(NetworkStream streaming)
{
string data = Console.ReadLine();
byte[] cpuorplayer = System.Text.Encoding.UTF8.GetBytes(data);
streaming.Write(cpuorplayer, 0, cpuorplayer.Length);
//games(streaming, colpal);
}
private void send(NetworkStream streaming, string data)
{
byte[] cpuorplayer = System.Text.Encoding.UTF8.GetBytes(data);
streaming.Write(cpuorplayer, 0, cpuorplayer.Length);
}
private string recv(NetworkStream streaming)
{
Byte[] bytes = new Byte[256];
int i = streaming.Read(bytes, 0, bytes.Length);
string data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
return data;
}
private string showpal(int col){
string show= "";
for(int i=0 ; i<col;i++){
show+="| ";
}
return "";
}
}