当前位置: 代码迷 >> J2SE >> JAVA 面试题,就大神帮忙解决方法
  详细解决方案

JAVA 面试题,就大神帮忙解决方法

热度:20   发布时间:2016-04-23 19:54:10.0
JAVA 面试题,就大神帮忙
There are two players to play a game with 4x4 positions.Each player can take one position for his turn,The winner is the fist one to take all of four positions in one line.
The figure shows three winning cases.
Let's make a simple application to make the judgment which player wins a game

 
------解决思路----------------------
利用二维数组可以解决,每走一步,统计该坐标的水平、垂直、对角线是否连续达到4个。下面是一个简单的demo:
public class Game {

private int[][] chessboard;
private boolean gameover = false;
private int winLength;
private int current;
private int total;

/**
 * @param size 棋盘大小
 * @param winLength 赢的连续数量
 */
public Game(int size,int winLength){
if(size<1
------解决思路----------------------
size<winLength){
throw new RuntimeException("棋局参数错误!");
}
chessboard = new int[size][size];
total = size*size;
this.winLength = winLength;
}

public int step(int v,int h,int player){
if(gameover
------解决思路----------------------
current==total){
System.out.println("游戏已经结束");
return 0;
}
if(chessboard[v][h]==0){
chessboard[v][h] = player;
current ++;
return judge(v, h, player);
}
return -1;
}

private int judge(int v,int h,int player){
int count = 0;
//判断水平方向是否满足4连
count += statsHorizontal(v, player, -1, h-1);
count += statsHorizontal(v, player, 1, h);
//前面和后面的数量达到4,胜出
if(count>=winLength){
win(count, player);
return count;
}
count = 0;

//判断垂直方向是否满足4连
count += statsVertical(h, player, -1, v-1);
count += statsVertical(h, player, 1, v);
if(count>=winLength){
win(count, player);
return count;
}
count = 0;

//对角线4连 \方向
count += statsDiagonal_1(v, h, player);
if(count>=winLength){
win(count, player);
return count;
}
count = 0;

//对角线4连 /方向
count += statsDiagonal_2(v, h, player);
if(count>=winLength){
win(count, player);
return count;
}

return count;
}

private void win(int count,int player){
System.out.println("Player "+player+" win");
gameover = true;
}

/**
 * 统计水平方向连续数量
 */
private int statsHorizontal(int v, int player,int step,int position) {
int count = 0;
for(int i=position;i>=0&&i<chessboard[v].length;i+=step){
if (chessboard[v][i] == player) {
count++;
continue;
}
break;
}
return count;
}

/**
 * 统计垂直方向连续数量
 */
private int statsVertical(int h,int player,int step,int position){
int count = 0;
for(int i=position;i>=0&&i<chessboard.length;i+=step){
if(chessboard[i][h]==player){
count++;
continue;
}
break;
}
return count;
}

private int statsDiagonal_1(int v,int h,int player){
int count=0;
for(int i=1;v-i>=0&&h-i>=0;i++){
if(chessboard[v-i][h-i] == player){
count++;
continue;
}
break;
}
for(int i=0;v+i<chessboard.length&&h+i<chessboard[v+i].length;i++){
if(chessboard[v+i][h+i] == player){
count++;
continue;
}
break;
}
return count;
}

private int statsDiagonal_2(int v,int h,int player){
int count=0;
for(int i=v;v+i<chessboard.length&&h-i>=0;i++){
if(chessboard[v+i][h-i] == player){
count++;
continue;
}
break;
}
for(int i=h;v-i>=0&&h+i<chessboard[v-i].length;i++){
if(chessboard[v-i][h+i] == player){
count++;
continue;
}
break;
}
return count;
}

public static void main(String[] args) {
Game g = new Game(5,4);
g.step(0, 0, 1);
g.step(1, 1, 1);
g.step(2, 2, 1);
g.step(3, 3, 1);
}
}

------解决思路----------------------

import java.util.Scanner;
public class winGame {
    //四个方向的h,s,x1,x2
static int whoWin = 0;
static boolean gameOver = false;
static int[][] a = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("Game starts with A player!");
while(!gameOver)
{
char[] c = scanner.next().toCharArray();
int a1 = Integer.parseInt(c[0]+"");
int a2 = Integer.parseInt(c[1]+"");
if(a1>3 
------解决思路----------------------
 a2 >3 
------解决思路----------------------
 a[a1][a2] == 1) 
{
System.out.println("replay!");
continue;
}
a[a1][a2] = 1;
if(HX(a1,a2 ) 
------解决思路----------------------
 FX(a1, a2) )
{
gameOver = true;
}
whoWin++;
}
if(whoWin % 2 == 1)
System.out.println("Winner is A");
else {
System.out.println("Winner is B");
}

}
//横竖方向直接判断
static public boolean HX(int i,int j)
{
int s1 = 1;
int s2 = 1;
for(int k = -3; k< 4;k++)
{
if(i+k >= 0 && i+k < 4) 
s1 = s1*a[i+k][j];
}
for(int k = -3; k<4;k++)
{
if(j+k >= 0 && j+k<4) 
s2 = s2*a[i][j+k];
}
if(s1 == 1 
------解决思路----------------------
 s2 == 1)
return true;
return false;
}
//斜方向判断
static public boolean FX(int i,int j)
{

int s1 = 1;
int s2 = 1;
//四次才说明该方向为有效方向
int times1 = 0;
int times2 = 0;
for(int k = -3; k<=3;k++)
{
if(i+k>= 0 && i+k<4 && j+k>=0 && j+k< 4) 
{
s1 = s1*a[i+k][j+k];
    times1++;
}
}
if(times1 !=4 ) s1 = 0;

for(int k = -3; k<=3;k++)
{
if(i+k>=0 && i+k<4 && j-k>=0 && j-k< 4) 
{
s2 = s2*a[i+k][j-k];
times2++;
}
    
}
if(times2 !=4 ) s2 = 0;
if(s1 == 1 
------解决思路----------------------
 s2 == 1)
return true;
return false;
}
}

  相关解决方案