【发布时间】:2014-07-18 03:03:23
【问题描述】:
你好,StackOverflow!
我正在编写流行游戏 Pentago 的 JavaScript 版本,作为一个业余项目,然后我必须在秋季重返课堂。不幸的是,我完全不知道如何从我目前的项目进展中继续前进。
我的方法
当我开始编写这个项目时,我认为这个游戏的最佳方法是实例化四个矩阵并将它们放在一个列表中。每个矩阵将代表 Pentago 板的一个部分。在考虑如何迭代矩阵并确定是否放置了一行 5 个游戏令牌时,我发现这种方法让我非常头疼。
重新评估我的方法
然后,我决定从外部角度了解如何解决此问题以使其更易于解决。有人建议我使用一个矩阵而不是四个矩阵。然而,这似乎确实使每个部分的旋转变得更加困难,但并非不合理。
我的问题...
说了这么多,我真正需要知道的是,在将“令牌”放置在某个位置后,我应该如何确定玩家是否赢得了比赛。
这是我认为我知道的:
确定游戏是否结束的函数将在玩家旋转一个象限后执行。
鉴于此,我将知道哪个玩家放置了最后一个标记。我只需要考虑那个玩家放置的代币。
我必须从放置最后一个标记的矩阵中的位置开始迭代矩阵,并确定一个标记是否与其相邻。然后,递归地对每个相邻的标记执行此操作,直到找到对角线、垂直或水平方向的五个相邻标记。
下面是我已经为这个游戏编写的代码。我真的很感激我能在项目的这一部分获得任何帮助......它真的让我很难过! 谢谢!
到目前为止的代码
https://github.com/jakenewby/pentagojs/tree/master/www/app
var game = (function (players){
var self = {};
self.numTokensPlaced = 0;
// instantiate the pentago board to all empty
self.board = [];
for(var i=0; i<6; i++)
{
self.board[i] = [];
for(var j=0; j<6; j++)
{
self.board[i][j] = null;
}
}
// rotate one of the four sections of the board
// given a section and a direction
self.rotateSection = function (section, direction)
{
if(isGameOver())
{
}
else
{
if(direction == 'clockwise')
{
/*
var newGrid = [];
var rowLength = Math.sqrt(section.length);
newGrid.length = section.length
for (var i = 0; i < section.length; i++)
{
//convert to x/y
var x = i % rowLength;
var y = Math.floor(i / rowLength);
//find new x/y
var newX = rowLength - y - 1;
var newY = x;
//convert back to index
var newPosition = newY * rowLength + newX;
newGrid[newPosition] = section[i];
}
for (var i = 0; i < newGrid.length; i++)
{
console.log(newGrid[i])
}
*/
}
else
{
// rotate counter clockwise
}
switchTurns();
}
}
// place a token at a place on the board given
// the player placing the token, the section, and the
// location coordinates in that section that the token
// is being placed.
self.placeToken = function(playerid, location)
{
if (self.board[location[0]][location[1]] != null)
{
return location.toString() + ' is already taken';
}
else
{
self.board[location[0]][location[1]] = playerid;
self.numTokensPlaced = self.numTokensPlaced + 1;
if(isGameOver())
{
alert('game is over!');
}
}
}
// looks at the current player, and sets the current
// player variable to the other player to switch turns.
function switchTurns()
{
if (players.current == players[0])
{
players.current = players[1];
}
else
{
players.current = players[0];
}
console.log(players.current);
}
// look through the board to see if there are any
// five tokens in a row belonging to one player.
// returns bool
function isGameOver(consecutiveTokens)
{
// game cannot be over if the number
// of tokens placed is less than 10
if(self.numTokensPlaced() >= 10)
{
if(consecutiveTokens < 5)
{
for(i=0; i < 3; i++)
{
if(self.board[i] != null)
{
for(j=0; j < 3; j++)
{
if(self.board[i][j] != null)
{
if(self.board[i][j] == players.current)
{
}
}
}
}
}
}
else
{
return true;
}
}
else
{
return false;
}
}
return self;
}());
游戏规则。
在象限中放置一个弹珠。将大理石放入其中一个象限后,将其中一个象限向任一方向旋转 90 度。将五个弹珠水平、垂直或对角对齐后,您就赢了!
【问题讨论】:
-
请在此处发布您的代码的相关 sn-p。我怀疑 github 链接很快就会断开,但是如果你不能挑选出一小部分代码来关注,你的问题可能太宽泛了。
-
我将创建一个包含 12 个函数的数组,这些函数遍历每个轴以找到 5 个连续的特定颜色的球;一旦你被击中,游戏结束......假设你不计算对角线:)
-
这篇文章是关于游戏的函数/方法,用于确定玩家在旋转棋盘的一部分后是否获胜。感谢您的建议!
-
杰克,它必须考虑对角线,所以... 15 个函数。我会给这个实现一些想法。感谢您的意见!
-
如果每个人都像这张海报那样花一半的时间来提出问题,那将是一个更好的地方。
标签: javascript algorithm matrix