Example #1
0
        /// <summary>
        /// returns a processed with map of current player including and map of opponent with ship positions hidden
        /// </summary>
        /// <param name="currentPlayer">current player</param>
        /// <param name="opponentPlayer">opponent player</param>
        /// <returns>processed string e.g. for console</returns>
        public static String Draw(Player currentPlayer, Player opponentPlayer)
        {
            int mapDim = opponentPlayer.map.Dimension;
            String line = "\n";

            line += DrawHeader(mapDim, opponentPlayer);
            line += drawResultRows(currentPlayer.map, opponentPlayer.map);

            line += "---+";
            for (int i = 0; i < mapDim; i++)
                line += "--";
            line += "-+---+";
            for (int i = 0; i < mapDim; i++)
                line += "--";
            line += "-+\n";
            line += "     O: Shot into water  X: Ship hit  S: Ship still intact\n";

            return line;
        }
Example #2
0
        /// <summary>
        /// returns string with header information of player and opponent map
        /// including column numbers
        /// </summary>
        /// <param name="mapDim">edge length of map</param>
        /// <returns>processed string</returns>
        private static String DrawHeader(int mapDim, Player opponentPlayer)
        {
            String oppWaters = opponentPlayer.name.ToUpper() + "S WATERS";
            String yourWaters = "YOUR WATERS";

            String line = "____";
            for (int i = 0; i < ((2 * mapDim - oppWaters.Length) / 2); i++)
                line += "_";
            line += oppWaters;
            for (int i = 0; i < ((2 * mapDim - oppWaters.Length) / 2); i++)
                line += "_";

            line += "______";
            for (int i = 0; i < ((2 * mapDim - yourWaters.Length) / 2); i++)
                line += "_";
            line += yourWaters;
            for (int i = 0; i < ((2 * mapDim - yourWaters.Length) / 2); i++)
                line += "_";
            line += "__\n";

            for (int row = (int)Math.Floor(Math.Log10(mapDim)); row >= 0; row--)
            {
                line += "   |";
                line += drawNumbersHorizontal(mapDim, row > 0);

                line += " |   |";
                line += drawNumbersHorizontal(mapDim, row > 0);

                line += " | \n";
            }

            line += "---+";
            for (int i = 0; i < mapDim; i++)
                line += "--";
            line += "-+---+";
            for (int i = 0; i < mapDim; i++)
                line += "--";
            line += "-+\n";

            return line;
        }
Example #3
0
		public static void Run()
		{
            // initialize
            int mapDimension = 16;
            Map mapServer = Map.GenerateShipsOnMap(new Map(mapDimension));
            Map mapHuman = Map.GenerateShipsOnMap(new Map(mapDimension));

            Player playerServer = new Player("Server", mapServer); // non-player-character ie our server
            Player playerHuman = new Player("Human", mapHuman); // player character
            int currentRound = 0;

            ArrayList sequences = initSequenceList("Scripts");
        

			NetPeerConfiguration config = new NetPeerConfiguration("xnaapp");
			config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
			config.Port = 14242;

			// create and start server
			NetServer server = new NetServer(config);
            try 
            {
			server.Start();

			// schedule initial sending of position updates
			double nextSendUpdates = NetTime.Now;

			// run until escape is pressed
			//while (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.Escape)
            while (true)

            {
				NetIncomingMessage msg;
				while ((msg = server.ReadMessage()) != null)
				{
					switch (msg.MessageType)
					{
						case NetIncomingMessageType.DiscoveryRequest:
							// Server received a discovery request from a client; send a discovery response (with no extra data attached)
							server.SendDiscoveryResponse(null, msg.SenderEndpoint);
							break;
						case NetIncomingMessageType.VerboseDebugMessage:
						case NetIncomingMessageType.DebugMessage:
						case NetIncomingMessageType.WarningMessage:
						case NetIncomingMessageType.ErrorMessage:
							// Just print diagnostic messages to console
							Console.WriteLine(msg.ReadString());
							break;
						case NetIncomingMessageType.StatusChanged:
							NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
							if (status == NetConnectionStatus.Connected)
							{
								// A new player just connected!
								Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " connected!");

                                // create new player object
                                // initialize new game

                                //playerServer.startGame();
                                //playerHuman.startGame();

							}

							break;
						case NetIncomingMessageType.Data:

                            String responseString = "Nothing happend";

                            // player round

							// The client sent input to the server
							
                            String sequenceName = msg.ReadString();

                            int round = msg.ReadInt32();
                            if (round > currentRound)
                            {
                                currentRound = round;

                                if (sequences.Contains(sequenceName))
                                {
                                
                                // choose and fire sequence

                                var executer = new SequenceExecuter(playerServer.map);
                                var resultMessagePlayer = executer.LoadScript(sequenceName);


                                // switch round to npc

                                // fire random sequence
                                int randomIndex = RandomNumber(0, sequences.Count);
                                sequenceName = (String)sequences[randomIndex];
                                executer = new SequenceExecuter(playerHuman.map);
                                var resultMessageNPC = executer.LoadScript(sequenceName);

                                // send response to client

                                
                                String battlefield = Battlefield.Draw(playerHuman, playerServer);
                                responseString = "Player: " + resultMessagePlayer + "\n" + battlefield + " \nComputer: " + resultMessageNPC;
                                } else {
                                    responseString = "FireSequence not recognized!";
                                }
                            } else {
                                responseString = "Game round invalid";
                            }



                            NetOutgoingMessage om = server.CreateMessage();
                            om.Write(responseString);
                            // send to every player, which is only one in our case
                            foreach (NetConnection player in server.Connections)
                            {
                                server.SendMessage(om, player, NetDeliveryMethod.Unreliable);
                            }

							break;
					} // switch messageType

				} // while readMessage

               

				// sleep to allow other processes to run smoothly
				Thread.Sleep(1);
			} // while thread

            }
            catch (SocketException ex)
            {
                System.Console.WriteLine("Socket could not be initiated, is the port already used?!");
                System.Windows.Forms.MessageBox.Show("Socket could not be initiated, is the port already used?!", 
                    "September 1983 Server");
            }

		}