/// <summary>Starts a game of Sea Battle, the type will depend on what the user has selected in the main menu.</summary> private static void RunGame() { Game.Game curGame; switch (selected) { case 0: curGame = new Game._1Player(SettingsEnabled[0], SettingsEnabled[1], SettingsEnabled[2], 10, 10, Shiplist, PlaneList); break; case 1: curGame = new Game._2Player(SettingsEnabled[0], SettingsEnabled[1], SettingsEnabled[2], 10, 10, Shiplist, PlaneList); break; case 2: curGame = new Game.LanHost(SettingsEnabled[0], SettingsEnabled[1], SettingsEnabled[2], 10, 10, Shiplist, PlaneList); break; case 3: curGame = new Game.LanClient(SettingsEnabled[0], SettingsEnabled[1], SettingsEnabled[2], 10, 10, Shiplist, PlaneList); break; default: return; } curGame.RunGame(); }
/// <summary>Runs a LAN game, here this computer is the client.</summary> public override void RunGame() { string buf = ""; Console.CursorVisible = true; IPAddress hostIP = null; do { Console.Clear(); Console.Write("Enter the ip of the host:"); try { hostIP = IPAddress.Parse(Console.ReadLine()); if (hostIP != null) { break; } } catch (Exception ex) { continue; } }while(true); Console.CursorVisible = false; TcpClient client = new TcpClient(hostIP.ToString(), Program.Port); if ((buf = LanHost.GetData(client)).Equals("")) { return; } string[] inps = buf.Split(LanHost.FieldDelimiter.ToCharArray()); bool clientSalvo = bool.Parse(inps[0]); bool clientBonus = bool.Parse(inps[1]); bool clientAdvanced = bool.Parse(inps[2]); SetUpMap temp = new SetUpMap(Player2, clientAdvanced); if (!LanHost.SendData(temp.ManualSetUp("Client", ShipList, PlaneList), client)) { return; } Console.Clear(); Console.WriteLine("Waiting for Host to finish setting up map."); if ((buf = LanHost.GetData(client)).Equals("")) { return; } temp = new SetUpMap(Player1, clientAdvanced); temp.TextSetUp(buf, ShipList, PlaneList); Turn t; do { Console.Clear(); Console.WriteLine("It's time for the host to play, please wait."); if ((buf = LanHost.GetData(client)).Equals("")) { return; } if (clientAdvanced) { t = new AdvancedTurn(clientBonus, clientSalvo, Player1, Player2); } else { t = new Turn(clientBonus, clientSalvo, Player1, Player2); } t.DoTextTurnSequence(buf); if (Player2.hasLost()) { PauseGame("The client has lost the game, press ESC to return to main menu."); client.Close(); return; } if (clientAdvanced) { t = new AdvancedTurn(clientBonus, clientSalvo, Player2, Player1); } else { t = new Turn(clientBonus, clientSalvo, Player2, Player1); } PauseGame("It's time for the client to play, press ESC to continue."); if (!LanHost.SendData(t.DoManualTurnSequence(), client)) { return; } if (Player1.hasLost()) { PauseGame("The client has won the game, press ESC to return to main menu."); client.Close(); return; } } while (true); }