博弈(类似于井字)算法需要。 [英] A Game (similar to tic tac toe) algorithm needed.

查看:128
本文介绍了博弈(类似于井字)算法需要。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我是初学Java编程。我想为学校项目的应用程序。我决定做一个棋盘游戏! pretty的直线前进。 ![董事会] [1]  这是董事会,并且有两个球员(人力)。第一个播放器(X)将他的举动在黑板上的任何地方。有提供给他9的地方。

first of all i'm beginner to Java programming. I want to make an application for School project. I have decided to make a board game! pretty straight forward. ![Board][1] This is the board and there are two player (Both Human). first one player(x) will make his move anywhere on the board. there are 9 places available to him.

本场比赛的动机是把所有三个标记在一行对角,水平或垂直。

motive of the game is to put all three tokens in one line diagonally, horizontally or vertically.

![获奖情况] [2] 我想算法检查赢家,如何挑选价值FRM用户时,他让一招。 我浏览大量的井字TOH算法,但我不知道该怎么去与他们错了。 ![更多钞票移动] [3] 请帮忙!!

![Winning condition][2] i want algorithm to check the winner and how to pick value frm user when he makes a move. i browsed lots of tic tac toh algorithm but i dont know what goes wrong with them. ![Posible moves][3] Please Help!!

推荐答案

由于它的功课,我不想只是给你一个答案。此外,你没有什么你都在努力是很清楚,所以我希望这是适用于您遇到的(S)的问题。编辑你的答案,使之更加清晰和/或发表评论,让所有谁回答可能会提高他们的答案更有效地满足您的需求。

您可以使用一个二维数组的存储板。在Java中,语法用于创建一个3x3的二维数组(例如,使用字符类型,存储 X 0 )如下:

You can make use of a two-dimensional array to store the board. In Java, the syntax for creating a 3x3 two-dimensional array (e.g. using the char type, to store X or O) is as follows:

char[][] board = new char[3][3];

在井字棋,有8种可能板上规定,表明玩家在游戏中获胜(标记 X 下面的动作):

In Tic-Tac-Toe, there are 8 possible board states that indicate that a player has won the game (moves marked with X below):

 (1)      (2)      (3)      (4)      (5)      (6)      (7)      (8)
X X X    # # #    # # #    X # #    # X #    # # X    X # #    # # X
# # #    X X X    # # #    X # #    # X #    # # X    # X #    # X #
# # #    # # #    X X X    X # #    # X #    # # X    # # X    X # #

记住,数组下标0开始(并上升到2,因为我们有一个3x3的二维数组),这相当于下面的一组条件:

Remembering that array indices start at 0 (and go up to 2, since we have a 3x3 2D array), this translates to the following set of conditions:

(1): board[0][0] == board[0][1] && board[0][1] == board[0][2]
(2): board[1][0] == board[1][1] && board[1][1] == board[1][2]
(3): board[2][0] == board[2][1] && board[2][1] == board[2][2]
(4): board[0][0] == board[1][0] && board[1][0] == board[2][0]
(5): board[0][1] == board[1][1] && board[1][1] == board[2][1]
(6): board[0][2] == board[1][2] && board[1][2] == board[2][2]
(7): board[0][0] == board[1][1] && board[1][1] == board[2][2]
(8): board[0][2] == board[1][1] && board[1][1] == board[2][0]

如果任何这些细胞是相等的(并且不为空),然后有人(无论是 X 0 ,这取决于谁占有这些细胞)已经赢得了比赛。你可能使用循环,使这个code更紧凑。

If any of these cells are equal (and are not blank) then someone (either X or O, depending on who occupies those cells) has won the game. You could potentially use loops to make this code more compact.

这篇关于博弈(类似于井字)算法需要。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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