static void Main(string[] args) { Program myProgram = new Program(); myProgram.Establish(); Game myGame = new Game(); myGame.getdatafromplayer1 = ReadFromConsole; myGame.getdatafromplayer2 = myProgram.ReceiveMsgFromClient; myGame.writemessagePlayer1 = WriteToConsole; myGame.writemessagePlayer2 = myProgram.SendMsgToClient; myGame.Go(); }
public bool initializeGame(Int32 starterplayerid, Int32 secondplayerid, Object gameField) { Game currgame = new Game(new ServerClient(lobby.getbyId(starterplayerid)), new ServerClient(lobby.getbyId(secondplayerid)),gameField); gamelist.Add(currgame); byte[] data = serializeObject(gameField); Socket socketsendto = clientsockets[secondplayerid - 1]; socketsendto.Send(data); byte[] id = serializeObject(starterplayerid); socketsendto.Send(id); currgame.gamestarted = true; return recieveReaction(socketsendto, 100); }
public static void TryCreateGame() { List<Client> tmp = clients.Where(x => x.search).ToList(); if (tmp.Count > 1) { if (tmp.Count % 2 == 1) { tmp.RemoveAt(tmp.Count - 1); } for (int i = 0; i < tmp.Count; i += 2) { Client[] newGamers = new Client[]{tmp[i], tmp[i+1]}; Game game = new Game(newGamers); } } }
public void RemoveGame(Game game) { foreach (string player in game.Players) { currentGames[player].Remove(game); } }
public static void ReceiveMessage(Socket s) { int k = s.Receive(b); Console.WriteLine("Received:"); string receivedMessage = ""; for (int i = 0; i < k; i++) receivedMessage += Convert.ToChar(b[i]); Console.WriteLine(receivedMessage); string[] tokens = receivedMessage.Split(' '); switch (tokens[0]) { case "isgameover?": { if (game.IsOver()) { Stats.AddVictory(); s.Send(enc.GetBytes("gameisover " + Stats.Victories)); } else s.Send(enc.GetBytes("gamenotover")); break; } case "start": { try { game = new Game(Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2]), Convert.ToInt32(tokens[3])); s.Send(enc.GetBytes("ok")); } catch (Exception e) { Console.WriteLine(e); } break; } case "leftclick": { string response = game.LeftClick( Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2])); //Console.WriteLine(response); s.Send(enc.GetBytes(response)); break; } case "elapsedtime": { s.Send(enc.GetBytes("elapsedtime " + game.time.ToString())); break; } case "rightclick": { string response = game.RightClick(Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2])); //Console.WriteLine(response); s.Send(enc.GetBytes(response)); break; } case "minesleft": { string response = "minesleft " + game.MinesLeft; s.Send(enc.GetBytes(response)); break; } } //s.Send(enc.GetBytes(receivedMessage.Split(' ')[0])); //Console.WriteLine("Sent Response"); }
public void ReceiveMessage(Socket s) { int k = s.Receive(buffer); StringBuilder receivedMessageBuilder = new StringBuilder(); for (int i = 0; i < k; i++) receivedMessageBuilder.Append((char)buffer[i]); string receivedMessage = receivedMessageBuilder.ToString(); if (receivedMessage != "elapsedtime" && receivedMessage != "isgameover?" && receivedMessage != "minesleft") { Console.Write("Received [" + s.RemoteEndPoint + "]: "); Console.WriteLine(receivedMessage); } string[] tokens = receivedMessage.Split(' '); switch (tokens[0]) { case "isgameover?": { if (game.IsOver()) { Stats.AddVictory(); s.Send(enc.GetBytes("gameisover " + Stats.Victories)); } else s.Send(enc.GetBytes("gamenotover")); break; } case "start": { try { game = new Game(Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2]), Convert.ToInt32(tokens[3])); s.Send(enc.GetBytes("ok")); } catch (Exception e) { Console.WriteLine(e.Message); } break; } case "leftclick": { string response = game.LeftClick( Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2])); //Console.WriteLine(response); s.Send(enc.GetBytes(response)); break; } case "elapsedtime": { s.Send(enc.GetBytes("elapsedtime " + game.time.ToString())); break; } case "rightclick": { string response = game.RightClick(Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2])); //Console.WriteLine(response); s.Send(enc.GetBytes(response)); break; } case "minesleft": { string response = "minesleft " + game.MinesLeft; s.Send(enc.GetBytes(response)); break; } } }
public static void GameOver(Game game) { games.Remove(game); }