井字游戏,帮助/确定优胜者 [英] Tic Tac Toe, Help/Determine Winner

查看:77
本文介绍了井字游戏,帮助/确定优胜者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个TicTacToe类,其中包含一个3 x 3的整数矩形数组&由2位人类玩家扮演。 1用于第一个玩家的移动&第二名玩家的移动使用 2。目前,我很困惑,不知道如何确定/检查游戏是否获胜,或者在完成每一个动作后是否平局。



不幸的是,我一直在检查每位玩家的举动后游戏是赢还是平局。希望有人可以提供帮助。



这是我到目前为止的代码:
主类:

 正在使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

名称空间ConsoleApplication1
{
类程序
{
static void Main(string [] args)
{
TicTacToe游戏=新的TicTacToe();
game.PrintBoard();
game.Play();
}
}
}

井字游戏类:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;


名称空间ConsoleApplication1
{
class TicTacToe
{
private const int BOARDSIZE = 3; //板子的大小
private int [,] board = new int [BOARDSIZE,BOARDSIZE]; //棋盘表示
字符串player1row,player1column,player2row,player2column;
枚举完成{win1,win2,win3,win4,win5,win6,win7,win8,
win9,win10,win11,win12,win13,win14,win15,win16};


//默认构造函数
public TicTacToe(){
board = new int [3,3] {{0,0,0},{0, 0,0},{0,0,0}};
}

int win1 = 1;

public void PrintBoard(){
Console.WriteLine( ----------------------- \n +
| | | | \n +
| {0} | {1} | {2} | \n +
| _______ | _______ | _______ | \n +
| | | | \n +
| {3} | {4} | {5} | \n +
| _______ | _______ | _______ | \n +
| | | | \n +
| {6} | {7} | {8} | \n +
| _______ | _______ | _______ | \n,
板[0,0],板[0,1],板[0,2],
板[1,0],板[1,1],board [1,2],
board [2,0],board [2,1],board [2,2]);
} //结束PrintBoard方法

public void Play(){
而(true)
{
Console.WriteLine(播放器1轮到。 );
Console.Write(播放器1:输入行(0< = row< 3):); //提示用户
player1row = Console.ReadLine(); //从用户
Console.Write(播放器1:输入列(0< =行< 3):)中获取字符串。 //提示用户
player1column = Console.ReadLine(); //从用户

中获取字符串//将字符串转换为int
int p1r = Convert.ToInt32(player1row);
int p1c = Convert.ToInt32(player1column);
//将标记分配到所需位置
board [p1r,p1c] = 1;

PrintBoard(); //更新板
checkWinner();

Console.WriteLine( \Player 2的回合。);
Console.Write(播放器2:输入行(0< = row< 3):);
player2row = Console.ReadLine(); //从用户
Console.Write(播放器2:输入列(0< =行< 3):)中获取字符串。
player2column = Console.ReadLine(); //从用户

中获取字符串//将字符串转换为int
int p2r = Convert.ToInt32(player2row);
int p2c = Convert.ToInt32(player2column);
//将标记分配到所需位置
board [p2r,p2c] = 2;

PrintBoard(); //更新板
checkWinner();

}
} //结束播放方法

私人布尔checkWinner()
{
for(int i = 0; i< ; 3; i ++)
for(int j = 0; j< 3; j ++)

//我想检查胜利还是抽签但是这里不知道$ b如何





$ b}} //最终舱

我想使用2 for循环检查获胜者,首先检查行,然后再次检查列,然后添加2个独立的if语句检查对角线,但我真的不知道不知道如何

解决方案

我不会为您编写代码,但这是您可以使用的逻辑:

1)对于每一列,检查所有行是否相同,如果是,则声明获胜者。如果不是,请转到步骤2。



2)对于每一行,检查所有列值是否相同,如果是,则声明获胜者。如果不是,请转到步骤3。



3)现在检查对角线,这里只有两种可能性:[0,0] [1,1] [2,2]和如果[0,2] [1,1] [2,0]是相同的声明获胜者,则还检查是否数组中的所有值都已填充;如果是,则声明draw;如果没有,则使用户输入值。


I'm creating a TicTacToe class that contains a 3 by 3 rectangular array of integers & is played by 2 human players. "1" is used for the first player's move & "2" is used for the second player's move. Currently, I am stuck and have no clue on how to determine/check whether the game has been won or whether its a draw after each and every move has been made.

Unfortunately, I'm stuck in checking if the game is won or drawn after each player makes a move & was hoping someone could help.

This is my code thus far: Main class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TicTacToe game = new TicTacToe();
            game.PrintBoard();
            game.Play();
        }
    }
}

TicTacToe class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1
{
    class TicTacToe
    {
        private const int BOARDSIZE = 3; //size of the board
        private int[,] board = new int [BOARDSIZE,BOARDSIZE]; // board representation
        string player1row, player1column, player2row, player2column;
        enum DONE {win1, win2, win3, win4, win5, win6, win7,win8,
                   win9, win10, win11, win12, win13, win14, win15, win16};


        //Default Constructor
        public TicTacToe(){
            board = new int [3,3] { {0,0,0},{0,0,0},{0,0,0} };
        }

        int win1 = 1;  

        public void PrintBoard(){
            Console.WriteLine("-----------------------\n"+
                              "|       |       |       |\n"+
                              "|   {0}   |   {1}   |   {2}   |\n" +
                              "|_______|_______|_______|\n"+
                              "|       |       |       |\n"+
                              "|   {3}   |   {4}   |   {5}   |\n" +
                              "|_______|_______|_______|\n"+
                              "|       |       |       |\n"+
                              "|   {6}   |   {7}   |   {8}   |\n" +
                              "|_______|_______|_______|\n",
                              board[0,0],board[0,1],board[0,2],
                              board[1,0],board[1,1],board[1,2],
                              board[2,0],board[2,1],board[2,2]);
        }// end PrintBoard method

        public void Play(){
            while (true)
            {
                Console.WriteLine("Player 1's turn.");
                Console.Write("Player 1: Enter row ( 0 <= row < 3 ): "); //prompt user
                player1row = Console.ReadLine(); //get string from user           
                Console.Write("Player 1: Enter column ( 0 <= row < 3 ): "); //prompt user 
                player1column = Console.ReadLine(); //get string from user

                //Convert string to ints
                int p1r = Convert.ToInt32(player1row);
                int p1c = Convert.ToInt32(player1column);
                // assign marker to desired position
                board[p1r, p1c] = 1;

                PrintBoard(); // Update board
                checkWinner();

                Console.WriteLine("\nPlayer 2's turn.");
                Console.Write("Player 2: Enter row ( 0 <= row < 3 ): ");
                player2row = Console.ReadLine(); //get string from user     
                Console.Write("Player 2: Enter column ( 0 <= row < 3 ): ");
                player2column = Console.ReadLine(); //get string from user

                //Convert string to ints
                int p2r = Convert.ToInt32(player2row);
                int p2c = Convert.ToInt32(player2column);
                // assign marker to desired position
                board[p2r, p2c] = 2;

                PrintBoard(); // Update board
                checkWinner();

            }
        }//end Play method

        private bool checkWinner()
        {
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)

                    //I WANT TO CHECK FOR A WIN OR DRAW HERE BUT DONT KNOW HOW TO






    }}// end class

I want to check for a winner using 2 for loops, the first checking the rows, then another checking for columns, then adding 2 separate if statements to check the diagonals, but i really don't know how to & have been stuck for a while now.

解决方案

I wont code it for you but here is the logic you can use:

1) For each column check if all rows are the same, if yes declare winner. If not go to step 2.

2) For each row check if all column values are the same, if yes declare winner. If not go to step 3.

3) Now check diagonals, there are only two possibilities here [0,0] [1,1] [2,2] and also [0,2] [1,1] [2,0] if they are the same declare winner, if not check whether all values in array are filled, if yes declare draw if not make users enter values.

这篇关于井字游戏,帮助/确定优胜者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆