**如何创建一个消息框来确定tictactoe游戏的赢家?** [英] **How can I create a message box determine the winner of a tictactoe game?**

查看:90
本文介绍了**如何创建一个消息框来确定tictactoe游戏的赢家?**的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我的计算机科学老师给了我们一个tictactoe游戏的代码。他希望我们用它来做的是在整个程序中输入一个代码来确定tictactoe游戏的赢家。问题是,我不知道从哪里开始。



如果代码太长,请抱歉。



 < span class =code-keyword>使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 Randell_tictactoe
{
public partial class Form1:Form
{
// 这是声明将用于创建板的变量的代码
Rectangle [,] tictactoe;
int [,] board;
int player = 0 ;
public Form1()
{
InitializeComponent();
board = new int [ 3 3 ];
tictactoe = new 矩形[ 3 3 ];
int i = 0 ;
int j = 0 ;
while (i < = 2
{
while (j < = 2
{
board [i,j] = 0 ;
j ++;
}
j = 0 ;
i ++;
}

}

private void Form1_Paint( object sender,PaintEventArgs e)
{
// < span class =code-comment>此代码将负责绘制tictactoe网格

int x,y;
x = tictactoe.GetLength( 0 );
y = tictactoe.GetLength( 1 );
Pen pn = new 笔(Color.Black, 2 );
Graphics g = e.Graphics;
int i = 0 ;
int j = 0 ;
int a,b;
a = ClientRectangle.Width;
b = ClientRectangle.Height;
int storex,storey;
while (i < = x - 1
{
while (j < = y - 1
{

tictactoe [i,j] = new Rectangle(i *(a / 3 ),j *(b / 3 ),a / 3 ,b / 3 );
g.DrawRectangle(pn,tictactoe [i,j]);


j ++;
}
j = 0 ;
i ++;
}
i = 0 ;
j = 0 ;

while (i < = x - 1
{
while (j < = y - 1
{
if (board [i, j] == 1
{
storex = tictactoe [i,j] .X;
storey = tictactoe [i,j] .Y;
g.DrawLine(pn,storex,storey,storex +(a / 3 ),storey + b / 3 );
g.DrawLine(pn,storex,storey + b / 3 ,storex +(a / 3 ),storey);
}
else if (board [i,j] == 2
{
g.DrawEllipse(pn,tictactoe [i,j] .X,tictactoe [i,j] .Y,a / < span class =code-digit> 3
,b / 3 );
}
j ++;
}
j = 0 ;
i ++;
}
ResizeRedraw = true ;
}

private void Form1_MouseDown( object sender,MouseEventArgs e)
{
// 此代码用于检查玩家点击的空间是否被(人)对手占用。
int i = < span class =code-digit> 0 ;
int j = 0 ;
while (i < = 2
{
while (j < = 2
{
if (board [i,j] == 0 && player == 0&& tictactoe [i,j] .Contains(eX,eY))
{
board [i,j ] = 1 ;
player = 1 ;
Invalidate();
}
else if (board [i,j] == 0 && player == 1 & tictactoe [i,j] .Contains(eX,eY) )
{
board [i,j] = 2 ;
player = 0 ;
Invalidate();
}
j ++;
}
j = 0 ;
i ++;
}
}
}
}

解决方案

这是设定的作业帮助你学习,这意味着我们不太可能给你很多帮助,除非你陷入困境。



我相信你的老师一定给了你有一些关于如何继续的线索,所以如果你遇到一个特定的问题,请先行先回来再回来。



如果没有尝试搜索c#tic tac toe...这里有一篇关于CodeProject的文章可以帮助


  private   bool  AllCellsFull()
{
for int i = 0 ; i < = 2 ; i ++)
{
for int j = 0 ; j < = 2 ; j ++)
{
if (board [i,j] == 0
return false ;
}
}
返回 true ;
}

private string GetWinner()
{
for int i = 0 ; i < = 2 ; i ++)
{
< span class =code-keyword> var check1 = Enumerable.Range( 0 3 )。选择(p = > board [p,i]);

if (!check1.Contains( 0 )&& check1 .Distinct()。Count()== 1
return check1.ElementAt(< span class =code-digit> 0 )== 1 X O的;

var check2 = Enumerable.Range( 0 3 )。选择(p = > board [i,p]);
if (!check2.Contains( 0 )&& check2.Distinct() .Count()== 1
return check2.ElementAt( 0 )== 1 X O的;
}

var check3 = Enumerable.Range( 0 3 )。选择(p = > board [p,p]);
if (!check3.Contains( 0 )&& check3.Distinct() .Count()== 1
return check3.ElementAt( 0 )== 1 X O的;

var check4 = Enumerable.Range( 0 3 )。选择(p = > board [p, 2 - p]);
if (!check4.Contains( 0 )&& check4.Distinct() .Count()== 1
return check4.ElementAt( 0 )== 1 X O的;

return Draw ;
}







然后在鼠标按下事件结束时;



 ... 
...
string 结果= GetWinner();
if (AllCellsFull()|| result!= Draw
MessageBox.Show( 游戏结果: +结果);
}


Hey guys, my computer science teacher gave us this code for a tictactoe game. What he wants us to do with it is to type in a code into the entire program that will determine the winner of the tictactoe game. The problem is, I don't know where to start.

P.S sorry if the code is so long.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Randell_tictactoe
{
    public partial class Form1 : Form
    {
        //This is the code that declares the variables that will be used to create the board
        Rectangle[,] tictactoe;
        int[,] board;
        int player = 0;
        public Form1()
        {
            InitializeComponent();
            board = new int[3, 3];
            tictactoe = new Rectangle[3, 3];
            int i = 0;
            int j = 0;
            while (i <= 2)
            {
                while (j <= 2)
                {
                    board[i, j] = 0;
                    j++;
                }
                j = 0;
                i++;
            }

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //This code will be in charge of painting the tictactoe grid
            int x, y;
            x = tictactoe.GetLength(0);
            y = tictactoe.GetLength(1);
            Pen pn = new Pen(Color.Black, 2);
            Graphics g = e.Graphics;
            int i = 0;
            int j = 0;
            int a, b;
            a = ClientRectangle.Width;
            b = ClientRectangle.Height;
            int storex, storey;
            while (i <= x - 1)
            {
                while (j <= y - 1)
                {

                    tictactoe[i, j] = new Rectangle(i * (a / 3), j * (b / 3), a / 3, b / 3);
                    g.DrawRectangle(pn, tictactoe[i, j]);


                    j++;
                }
                j = 0;
                i++;
            }
            i = 0;
            j = 0;

            while (i <= x - 1)
            {
                while (j <= y - 1)
                {
                    if (board[i, j] == 1)
                    {
                        storex = tictactoe[i, j].X;
                        storey = tictactoe[i, j].Y;
                        g.DrawLine(pn, storex, storey, storex + (a / 3), storey + b / 3);
                        g.DrawLine(pn, storex, storey + b / 3, storex + (a / 3), storey);
                    }
                    else if (board[i, j] == 2)
                    {
                        g.DrawEllipse(pn, tictactoe[i, j].X, tictactoe[i, j].Y, a / 3, b / 3);
                    }
                    j++;
                }
                j = 0;
                i++;
            }
            ResizeRedraw = true;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            //This code is used to check whether the space the player clicks on is occupied by the (human)opponent or not.
            int i = 0;
            int j = 0;
            while (i <= 2)
            {
                while (j <= 2)
                {
                    if (board[i, j] == 0 && player == 0&& tictactoe[i,j].Contains(e.X,e.Y))
                    {
                        board[i, j] = 1;
                        player = 1;
                        Invalidate();
                    }
                    else if (board[i, j] == 0 && player == 1 & tictactoe[i, j].Contains(e.X, e.Y))
                    {
                        board[i, j] = 2;
                        player = 0;
                        Invalidate();
                    }
                    j++;
                }
                j = 0;
                i++;
            }
        }
    }
}

解决方案

This is homework which is set to help YOU to learn, which means we are unlikely to give you much help unless you get stuck on a particular point.

I'm sure your teacher must have given you some clues as to how to proceed, so have a go first and come back if you get stuck on a particular problem.

Failing that try searching for "c# tic tac toe" ... there is actually an article here on CodeProject that would help


private bool AllCellsFull()
{
    for (int i = 0; i <= 2; i++)
    {
        for (int j = 0; j <= 2; j++)
        {
            if (board[i, j] == 0)
                return false;
        }
    }
    return true;
}

private string GetWinner()
{
    for (int i = 0; i <= 2; i++)
    {
        var check1 = Enumerable.Range(0, 3).Select(p => board[p, i]);

        if (!check1.Contains(0) && check1.Distinct().Count() == 1)
            return check1.ElementAt(0) == 1 ? "X" : "O";

        var check2 = Enumerable.Range(0, 3).Select(p => board[i, p]);
        if (!check2.Contains(0) && check2.Distinct().Count() == 1)
            return check2.ElementAt(0) == 1 ? "X" : "O";
    }

    var check3 = Enumerable.Range(0, 3).Select(p => board[p, p]);
    if (!check3.Contains(0) && check3.Distinct().Count() == 1)
        return check3.ElementAt(0) == 1 ? "X" : "O";

    var check4 = Enumerable.Range(0, 3).Select(p => board[p, 2 - p]);
    if (!check4.Contains(0) && check4.Distinct().Count() == 1)
        return check4.ElementAt(0) == 1 ? "X" : "O";

    return "Draw";
}




Then at the end of your mouse down event;

...
...
           string result = GetWinner();
           if (AllCellsFull() || result != "Draw")
               MessageBox.Show("Game Result: " + result);
       }


这篇关于**如何创建一个消息框来确定tictactoe游戏的赢家?**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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