protected override GameState <TicTacToeState> cloneType()
    {
        TicTacToeGameState cloned = new TicTacToeGameState();

        cloned.board = ((int[, ])board.Clone());
        return(cloned);
    }
 private void LoadGame(GameInstance gameInstanceWithPreviousState)
 {
     if (gameInstanceWithPreviousState != null)
     {
         _gameState = JsonSerializer.Deserialize <TicTacToeGameState>(gameInstanceWithPreviousState.State.DataAsJson);
     }
 }
Example #3
0
        public ActionResult Index(int row, int col)
        {
            TicTacToeGameState state = (TicTacToeGameState)Session["state"];

            state.addMove(row, col);

            return(null);
        }
Example #4
0
    public void resetGame()
    {
        gameStarted = false;

        game       = new TicTacToeGameState();
        playerTurn = 0;
        ai.reset();

        aiStart.gameObject.SetActive(true);
        playerStart.gameObject.SetActive(true);
        resetButton.gameObject.SetActive(false);
    }
        private void SetupGame()
        {
            var playerUsers = _gameInstanceUsers.Where(x => x.Role == GameInstanceRoles.Player.ToString());

            _gameState = new TicTacToeGameState
            {
                HasGameBeenSetup = true,
                Board            = new string[9],
                xIsNext          = true,
                Player1          = CreatePlayer(playerUsers.ElementAt(0)),
                Player2          = CreatePlayer(playerUsers.ElementAt(1)),
            };
            _response.Messages.Add("Welcome to Tic-Tac-Toe!");
        }
Example #6
0
        public async Task TicTacToeAsync(IGuildUser user)
        {
            var message = await ReplyAsync($"<@{Context.User.Id}> Challenged <@{user.Id}> to TicTacToe.");

            Emoji[] emotes = new Emoji[9] {
                new Emoji("\u0031\uFE0F\u20E3"), new Emoji("\u0032\uFE0F\u20E3"), new Emoji("\u0033\uFE0F\u20E3"), new Emoji("\u0034\uFE0F\u20E3"), new Emoji("\u0035\uFE0F\u20E3"), new Emoji("\u0036\uFE0F\u20E3"), new Emoji("\u0037\uFE0F\u20E3"), new Emoji("\u0038\uFE0F\u20E3"), new Emoji("\u0039\uFE0F\u20E3")
            };
            await message.AddReactionsAsync(emotes);

            var gameState = new TicTacToeGameState(message, Context.User.Id, user.Id);

            ReactionService.AddTicTacToe(gameState);
            await message.ModifyAsync(m => { m.Content = gameState.ToString(); });
        }
    public void adjustRules(GameState <TicTacToeState> state)
    {
        TicTacToeGameState tttState = (TicTacToeGameState)state;
        List <PlayerAction <TicTacToeState> > actions = new List <PlayerAction <TicTacToeState> > ();

        for (int x = 0; x < tttState.board.GetLength(0); x++)
        {
            for (int y = 0; y < tttState.board.GetLength(1); y++)
            {
                if (tttState.board [x, y] == 0)
                {
                    Mark mark = new Mark();
                    mark.x = x;
                    mark.y = y;
                    actions.Add(mark);
                }
            }
        }

        List <TicTacToeState> list = new List <TicTacToeState>();

        if (state.parentGameState != null &&
            state.players.Length > state.parentGameState.possibleActions.Length)
        {
            int playerNum = 1;
            foreach (Player <TicTacToeState> player in state.players)
            {
                TicTacToeState pos = new TicTacToeState();

                pos.x = ((Mark)player.lastAction).x;
                pos.y = ((Mark)player.lastAction).y;

                if (!list.Contains(pos))
                {
                    list.Add(pos);
                    tttState.board[pos.x, pos.y] = playerNum;
                }
                playerNum++;
            }
        }


        tttState.possibleActions = actions.ToArray();
        TicTacToeGame.checkWin(tttState);
    }
Example #8
0
        // GET: TicTacToe
        public ActionResult Index()
        {
            if (Session["size"] == null)
            {
                return(RedirectToAction("Home", "Index"));
            }
            else
            {
                int           size  = (Int32)Session["size"];
                GameViewModel model = new GameViewModel(size);

                // put game state in session
                // Game begins with O
                Session["state"] = new TicTacToeGameState(size, false);

                return(View(model));
            }
        }
Example #9
0
        private TicTacToeMove CheckForWin(TicTacToeGameState gameState, TicTacToeBoardValue boardValue)
        {
            IEnumerable <TicTacToeMove> openMoves = gameState.Board.GetOpenMoves();

            foreach (TicTacToeMove move in openMoves)
            {
                gameState.Board.MakeMove(move, boardValue);

                TicTacToeBoardValue?winner = gameState.Board.CheckForWinner();
                if (winner == boardValue)
                {
                    return(move);
                }

                gameState.Board.ResetPosition(move.X, move.Y);
            }

            return(null);
        }
Example #10
0
        public override TicTacToeMove NextMove(TicTacToeGameState gameState)
        {
            // Can I win?
            TicTacToeMove winningMove = this.CheckForWin(gameState, this.BoardValue);

            // Can they win?
            TicTacToeBoardValue otherBoardValue = this.BoardValue == TicTacToeBoardValue.O ?
                                                  TicTacToeBoardValue.X : TicTacToeBoardValue.O;
            TicTacToeMove blockingMove = this.CheckForWin(gameState, otherBoardValue);

            // Can I play a corner?
            TicTacToeMove cornerMove = new TicTacToeMove[]
            {
                new TicTacToeMove(0, 0),
                new TicTacToeMove(0, 2),
                new TicTacToeMove(2, 0),
                new TicTacToeMove(2, 2)
            }.OrderBy(move => Guid.NewGuid()).FirstOrDefault(move => gameState.Board.IsValidMove(move));

            // play best move
            return(winningMove ?? blockingMove ?? cornerMove ?? base.NextMove(gameState));
        }
Example #11
0
        //this function asks the helper about the game state
        void ShowGameState()
        {
            //first we need to convert the text to a form that the helper understands
            char[] text = label5.Text.ToCharArray();

            List <TicTacToeFieldState> board = new List <TicTacToeFieldState>();

            for (int i = 0; i < text.Length; i++)
            {
                try
                {
                    TicTacToeFieldState fieldState = TicTacToeHelper.GetFieldFromChar(text[i]);
                    board.Add(fieldState);
                }
                catch
                {
                }
            }

            //get the game state
            TicTacToeGameState gameState = helper.CheckGameState(board.ToArray());

            if (gameState == TicTacToeGameState.Continuable)
            {
                label7.Text = "game is continuable.";
            }
            else if (gameState == TicTacToeGameState.Tied)
            {
                label7.Text = "game is tied.";
            }
            else if (gameState == TicTacToeGameState.XWon)
            {
                label7.Text = "x has won the game.";
            }
            else if (gameState == TicTacToeGameState.OWon)
            {
                label7.Text = "o has won the game.";
            }
        }
 private void OnExitState(TicTacToeGameState toExitState, TicTacToeGameState nextState)
 {
     toExitState.onExit.Invoke();
 }
 private void OnEnterState(TicTacToeGameState toEnterState, TicTacToeGameState prevState)
 {
     toEnterState.onEnter.Invoke();
 }
Example #14
0
    static TwoPlayersGameStats TicTacToeEval(Tuple <string, int> p1, Tuple <string, int> p2, int no_matches = 100)
    {
        TicTacToeGameState  state = new TicTacToeGameState();
        TwoPlayersGameStats stats = new TwoPlayersGameStats();

        ITicTacToeSimulationStrategy player1;
        ITicTacToeSimulationStrategy player2;


        /// set up experiment parameters and labels
        string label1 = p1.Item1;
        string label2 = p2.Item1;

        if (p1.Item2 < 0)
        {
            player1 = TicTacToePlayer(p1.Item1);
        }
        else
        {
            player1 = TicTacToePlayer(p1.Item1, p1.Item2);
            label1  = label1 + "(" + p1.Item2 + ")";
        }

        if (p2.Item2 < 0)
        {
            player2 = TicTacToePlayer(p2.Item1);
        }
        else
        {
            player2 = TicTacToePlayer(p2.Item1, p2.Item2);
            label2  = label2 + "(" + p2.Item2 + ")";
        }

        //Console.WriteLine (label1 + " vs " + label2 + "\n");

        int move_counter = 0;

        IGameMove current_move;

        stats.Reset();

        for (int p = 0; p < no_matches; p++)
        {
            int start_player = ((p % 2 == 0) ? 1 : 2);
            state.Restart(start_player);

            move_counter = 0;

            //// ERRORE NELLA SELEZIONE DEL GIOCATORE! :)

            while (!state.EndState())
            {
                // when player1 starts it plays even move (0 -> the first, 2 -> the third, etc.)
                if (start_player == 1)
                {
                    if (move_counter % 2 == 0)
                    {
                        current_move = player1.selectMove(state);
                    }
                    else
                    {
                        current_move = player2.selectMove(state);
                    }

                    // when player2 starts it plays even move (0 -> the first, 2 -> the third, etc.)
                }
                else
                {
                    if (move_counter % 2 == 0)
                    {
                        current_move = player2.selectMove(state);
                    }
                    else
                    {
                        current_move = player1.selectMove(state);
                    }
                }

                move_counter = move_counter + 1;

                state.DoMove(current_move);
            }
            //Console.WriteLine (p + "\tWINNER\t" + state.Winner() + "\t" + stats.PrettyPrintRates());
            if (state.Winner() == 1 || state.Winner() == 2)
            {
                stats.PlayerWon(state.Winner(), start_player);
            }
            else
            {
                stats.PlayersTied();
            }
        }

//		Console.WriteLine (player1.getFriendlyName() + " vs " + player2.getFriendlyName()+"\n");
//		Console.WriteLine ("P1WINS\t" + stats.Wins(1) + "\tP2WINS\t" +  + stats.Wins(2) + "\tTIES\t" + stats.Ties()+"\n\n");
//		Console.WriteLine ("\tP1-1ST\tP2-1S\nP1\t" + stats.Wins (1, 1) + "\t" + stats.Wins (1, 2) + "\nP2\t" + stats.Wins (2, 1) + "\t" + stats.Wins (2, 2) + "\n");

        return(stats);
    }
Example #15
0
        public override TicTacToeMove NextMove(TicTacToeGameState gameState)
        {
            Console.Clear();

            Console.WriteLine($"Player {this.Name}'s turn:");
            Console.WriteLine();
            Console.WriteLine();

            gameState.Board.Print();

            IEnumerable <TicTacToeMove> openMoves = gameState.Board.GetOpenMoves();

            TicTacToeMove selectedMove = openMoves.First();

            SetCursorToMove(selectedMove);

            while (true)
            {
                ConsoleKey key = Console.ReadKey().Key;

                switch (key)
                {
                case ConsoleKey.UpArrow:
                    selectedMove = openMoves
                                   .OrderByDescending(move => move.Y < selectedMove.Y)
                                   .ThenByDescending(move => move.X == selectedMove.X)
                                   .ThenByDescending(move => move.Y)
                                   .First();
                    break;

                case ConsoleKey.DownArrow:
                    selectedMove = openMoves
                                   .OrderByDescending(move => move.Y > selectedMove.Y)
                                   .ThenByDescending(move => move.X == selectedMove.X)
                                   .ThenBy(move => move.Y)
                                   .First();
                    break;

                case ConsoleKey.LeftArrow:
                    selectedMove = openMoves
                                   .OrderByDescending(move => move.X < selectedMove.X)
                                   .ThenByDescending(move => move.Y == selectedMove.Y)
                                   .ThenByDescending(move => move.X)
                                   .First();
                    break;

                case ConsoleKey.RightArrow:
                    selectedMove = openMoves
                                   .OrderByDescending(move => move.X > selectedMove.X)
                                   .ThenByDescending(move => move.Y == selectedMove.Y)
                                   .ThenBy(move => move.X)
                                   .First();
                    break;

                case ConsoleKey.Enter:
                    Console.Clear();
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine();
                    return(selectedMove);
                }

                SetCursorToMove(selectedMove);
            }
        }
Example #16
0
 void Start()
 {
     game = new TicTacToeGameState();
     ai   = new GameTreeAI <TicTacToeState> (
         Application.dataPath + "/TicTacToeTree.json", 0, new TicTacToeEvaluator());
 }
Example #17
0
    public static List <TicTacToeState> checkWin(TicTacToeGameState state)
    {
        List <TicTacToeState> states = new List <TicTacToeState>();

        for (int player = 0; player < state.players.Length; player++)
        {
            bool win = false;
            for (int x = 0; x < state.board.GetLength(0); x++)
            {
                for (int y = 0; y < state.board.GetLength(1); y++)
                {
                    if (state.board [x, y] != player + 1)
                    {
                        win    = false;
                        states = new List <TicTacToeState>();
                        break;
                    }

                    TicTacToeState s = new TicTacToeState();
                    s.x = x;
                    s.y = y;
                    states.Add(s);
                    win = true;
                }

                if (win)
                {
                    state.winState = true;
                    state.players[player].winner = true;
                    return(states);
                }
            }

            for (int y = 0; y < state.board.GetLength(1); y++)
            {
                for (int x = 0; x < state.board.GetLength(0); x++)
                {
                    if (state.board [x, y] != player + 1)
                    {
                        win    = false;
                        states = new List <TicTacToeState>();
                        break;
                    }

                    TicTacToeState s = new TicTacToeState();
                    s.x = x;
                    s.y = y;
                    states.Add(s);
                    win = true;
                }

                if (win)
                {
                    state.winState = true;
                    state.players[player].winner = true;
                    return(states);
                }
            }

            for (int x = 0; x < state.board.GetLength(0) && x < state.board.GetLength(1); x++)
            {
                if (state.board [x, x] != player + 1)
                {
                    win    = false;
                    states = new List <TicTacToeState>();
                    break;
                }

                TicTacToeState s = new TicTacToeState();
                s.x = x;
                s.y = x;
                states.Add(s);
                win = true;
            }

            if (win)
            {
                state.winState = true;
                state.players[player].winner = true;
                return(states);
            }

            for (int x = state.board.GetLength(0) - 1; x >= 0; x--)
            {
                if (state.board [x, x] != player + 1)
                {
                    win    = false;
                    states = new List <TicTacToeState>();
                    break;
                }

                TicTacToeState s = new TicTacToeState();
                s.x = x;
                s.y = x;
                states.Add(s);
                win = true;
            }

            if (win)
            {
                state.winState = true;
                state.players[player].winner = true;
                return(states);
            }
        }

        return(new List <TicTacToeState>());
    }
Example #18
0
        public TicTacToeGameState CheckGameState(TicTacToeFieldState[] board)
        {
            if (board.Length != BoardLength * BoardLength)
            {
                throw new ArgumentOutOfRangeException("board", "The array should be as long as the board!");
            }

            //first assume the game is tied
            TicTacToeGameState gameState = TicTacToeGameState.Tied;

            //check if the game is continuable
            for (int i = 0; i < board.Length; i++)
            {
                //if there's an empty field, the game is continuable.
                if (board[i] == TicTacToeFieldState.None)
                {
                    gameState = TicTacToeGameState.Continuable;
                    break;
                }
            }

            //check if x or o has won the game
            for (int i = 0; i < wins.GetLength(0); i++)
            {
                TicTacToeFieldState firstField = board[wins[i, 0]];

                //no need to do the win check if the field we picked is empty
                if (firstField != TicTacToeFieldState.None)
                {
                    //assume this is going to be a victory line
                    bool victory = true;

                    for (int k = 1; k < wins.GetLength(1); k++)
                    {
                        TicTacToeFieldState field = board[wins[i, k]];

                        //if selected field isn't the same as the first picked one,
                        //then this wasn't a winning line.
                        if (firstField != field)
                        {
                            victory = false;
                            break;
                        }
                    }

                    //if a winning line was found, change gameState
                    if (victory)
                    {
                        if (firstField == TicTacToeFieldState.X)
                        {
                            gameState = TicTacToeGameState.XWon;
                        }
                        else
                        {
                            gameState = TicTacToeGameState.OWon;
                        }
                        break;
                    }
                }
            }

            //the true game state is found and we can return it
            return(gameState);
        }
Example #19
0
 public void AddTicTacToe(TicTacToeGameState gs)
 {
     Games.Add(gs.gameMessage.Id, gs);
 }
 public override TicTacToeMove NextMove(TicTacToeGameState gameState)
 {
     return(gameState.Board.GetOpenMoves()
            .OrderBy(x => Guid.NewGuid())
            .FirstOrDefault());
 }
    void Start()
    {
        TicTacToeGameState state = new TicTacToeGameState();

        GameState <TicTacToeState> .generateJsonGameTree(state, "TicTacToeTree", 0);
    }