Example #1
0
        static void CommunicateWithClient(object objData)
        {
            ClientData clientData = (ClientData)objData;

            string input;

            while ((input = TransferHelper.Receive(clientData.ClientSocket)) != null)
            {
                Console.WriteLine(input);
                string[] splittedData = input.Split('/');
                if (splittedData[0] == "button")
                {
                    _game.Move((int)Char.GetNumericValue(splittedData[1][1]),
                               (int)Char.GetNumericValue(splittedData[1][2]), clientData.Name == Game.TicTacToe.Players.X);
                }
                else if (splittedData[0] == "restart")
                {
                    SendAll("restart");
                    if (_isOConnected && _isXConnected)
                    {
                        SendAll("start");
                    }
                    TransferHelper.Send("turn/false", clientData.ClientSocket);
                    _game = new Game.TicTacToe();
                }

                foreach (ClientData client in _clients)
                {
                    TransferHelper.Send(input, client.ClientSocket);
                    if (clientData != client)
                    {
                        TransferHelper.Send("turn/true", client.ClientSocket);
                    }

                    if (_game.IsWinner(clientData.Name == Game.TicTacToe.Players.X))
                    {
                        TransferHelper.Send("win/" + clientData.Name + " wins the game", client.ClientSocket);
                    }
                    else if (_game.IsDraw())
                    {
                        TransferHelper.Send("draw", client.ClientSocket);
                    }
                }
            }

            clientData.ClientSocket.Shutdown(SocketShutdown.Both);
            clientData.ClientSocket.Close();
            _clients.Remove(clientData);
            Console.WriteLine($"{clientData.Name} disconnected.");

            SendAll("disconnected");
            if (clientData.Name == Game.TicTacToe.Players.X)
            {
                _isXConnected = false;
            }
            else
            {
                _isOConnected = false;
            }
        }
Example #2
0
        // Application entry point
        public static void Main(string[] args)
        {
            bool play = true;

            while (play)
            {
                //Instantiate
                TicTacToe.Play();

                //Ask the user if they want to play again
                play = PlayAgain();
            }

            //Exit Message
            Console.WriteLine("Maybe next time! Have a great day!");
        }
Example #3
0
        // Application entry point
        public static void Main(string[] args)
        {
            //Need a bool to allow the user to play multiple times if they choose.
            bool playTicTacToe = true;

            while (playTicTacToe)
            {
                //Instantiate
                TicTacToe.Play();

                //Play Again?
                playTicTacToe = PlayAgain();
            }

            //Exit message
            Console.WriteLine("Maybe next time. Have a great day!");
        }
Example #4
0
        // Application entry point
        static void Main(string[] args)
        {
            bool play = true;

            while (play)
            {
                //instantiate
                TicTacToe.Play();

                //Play Againg
                play = PlayAgain();
            }

            //Create an exit message
            Console.WriteLine("OK! This was fun");
            Console.WriteLine("Can't wait to play again!");
            Console.WriteLine("Have a great day!!");
        }
Example #5
0
        public static void Main()
        {
            // Makes new game instance
            TicTacToe game = new TicTacToe();

            // Main game loop
            while (game.running)
            {
                // Checks which player playing TRUE if X false if O
                char playing = game.turn ? 'X' : 'O';

                // Shows current state of board and which player's turn it is
                Console.WriteLine("\n" + playing + " TURN!\n");
                Console.WriteLine(game.board.toString());

                int x = -1;

                // Gets new X position from player while their input is not valid
                while (x < 0 || x > 3)
                {
                    Console.Write("Enter x position: ");
                    x = Convert.ToInt32(Console.ReadLine()) - 1;
                }

                int y = -1;

                // Gets new Y position from player while their input is not valid
                while (y < 0 || y > 3)
                {
                    Console.Write("Enter y position: ");
                    y = Convert.ToInt32(Console.ReadLine()) - 1;
                }

                // If the move they want to make is not valid, don't make the move. And that
                // player has to go again when the game loops.
                if (game.board.isValid(x, y))
                {
                    // change who's turn it is
                    game.turn = !game.turn;

                    // make the move
                    game.board.makePlay(x, y, playing);

                    // check if the move the player just made was a winning move, if so, stop the game
                    // loop and set the winner.
                    if (game.board.checkWinner(playing))
                    {
                        game.running = false;
                        game.winner  = playing.ToString();
                    }

                    // if board is full and there was no winner, stop the game loop and set winner
                    // to NONE
                    if (game.board.isFull() && game.running)
                    {
                        game.running = false;
                        game.winner  = "NONE";
                    }
                }
            }

            // Prints final state of board
            Console.WriteLine("\n" + game.board.toString());

            // Prints winner
            Console.WriteLine("WINNER: " + game.winner);
        }
Example #6
0
        static void Main(string[] args)
        {
            string    hostName  = Dns.GetHostName();
            IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

            foreach (var ip in Dns.GetHostEntry(hostName).AddressList)
            {
                if (!ip.ToString().Contains(":") && !ip.ToString().Equals("127.0.0.1"))
                {
                    ipAddress = ip;
                }
            }

            int port = int.Parse(GetData("port"));

            Console.WriteLine($"IP address: {ipAddress}");

            IPEndPoint ipPoint = new IPEndPoint(ipAddress, port);
            Socket     socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Bind(ipPoint);
            socket.Listen(10);

            while (true)
            {
                if (!_isXConnected || !_isOConnected)
                {
                    Socket clientSocket = socket.Accept();
                    if (clientSocket != null)
                    {
                        ClientData data = null;
                        if (!_isXConnected)
                        {
                            _isXConnected = true;
                            data          = new ClientData(clientSocket,
                                                           Game.TicTacToe.Players.X);
                            TransferHelper.Send("playerSign/" + data.Name, clientSocket);
                        }
                        else
                        {
                            _isOConnected = true;
                            data          = new ClientData(clientSocket,
                                                           Game.TicTacToe.Players.O);
                            TransferHelper.Send("playerSign/" + data.Name, clientSocket);
                        }

                        foreach (ClientData client in _clients)
                        {
                            if (client != data)
                            {
                                TransferHelper.Send("turn/true", client.ClientSocket);
                            }
                            else
                            {
                                TransferHelper.Send("turn/false", client.ClientSocket);
                            }
                        }

                        Console.WriteLine($"{data.Name} connected");
                        _clients.Add(data);

                        Thread thread = new Thread(CommunicateWithClient);
                        thread.Start(data);

                        if (_isOConnected && _isXConnected)
                        {
                            SendAll("start");
                            _game = new Game.TicTacToe();
                        }
                    }
                }
            }
        }
 static void Main(string[] args)
 {
     TicTacToe.RunGame(PlayerType.Player, PlayerType.Random, Board.DEFAULT_SIZE);
     Console.ReadLine();
 }