| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 998 人关注过本帖
标题:JAVA的一个小游戏的编程问题 (第2题)
只看楼主 加入收藏
jackyshen
Rank: 2
等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:8 
JAVA的一个小游戏的编程问题 (第2题)
5-下,8-上,4-左,6-右,0-退出
如果按照上面的要求,长度是5,高度是3,那就是:
10000
00000
00000

输入5的话就是:
10000
10000
00000

输入6是:
10000
11000
00000
按这个玩,如果移动旁边没有数字了像:
10000
00000
00000
输入了4,没有数字了,就print left wall.如果右边没数字就print right wall.


10000
10000
00000
输入5(下),就变成
10000
20000
00000

像这样应该怎么做
import java.util.Scanner;

public class Grid
{
  int maze[][]; // the 2D maze
  int width;    // width of maze
  int height;   // height of maze
  int hPos;     // current horizontal position in maze
  int vPos;     // current vertical position in maze




   /*
    *  initializes maze using w (width) and h (height)
    */
  public Grid(int w, int h)
  {
      // set the width and height of maze
    width=w;
    height=h;

      // set the starting horizontal & vertical position in maze
    hPos=0;   
    vPos=0;

      // create 2D maze of specified size
    maze = new int[height][width];

    visit();  // visit the current position
  }


   /*
    *  increments the current position in maze by one
    *  then displays the maze (note that if the value
    *  reaches ten the value should reset back to zero
    */
  public void visit()
  {
      
        }



    /*
     *  print the maze values out as a 2 dimensional grid
     *  with width x height dimensions
     *  eg if the width is 4 and the height is 3 and only the top
     *  left position has been visited, it should print the following:
     *  
     *  1000
     *  0000
     *  0000
     */
  public void display()
  {
    //public static void main(String[] args) {
      // TODO Auto-generated method stub
      Scanner sc= new Scanner(System.in);
      //int width =sc.nextInt();
     // int height = sc.nextInt();
      int i,j;
      for(i=0;i<height;i++)
      {
       if(i==0)
        {
         System.out.print("1");
         for(j=1;j<width;j++)
         {
          System.out.print("0");
         }
         System.out.println();
        }
       else
        {
         for(j=0;j<width;j++)
         {
          System.out.print("0");
         }
         System.out.println();
        }

      }
  }


   /*
    *  move the current position in maze left by one position
    *  needs to avoid going through left wall
    */
  public void left()
  {
  }


   /*
    *  move the current position in maze right by one position
    *  needs to avoid going through right wall
    */
  public void right()
  {
  }


   /*
    *  move the current position in maze up by one position
    *  needs to avoid going through top wall
    */
  public void up()
  {
  }


   /*
    *  move the current position in maze down by one position
    *  needs to avoid going through bottom wall
    */
  public void down()
  {
  }
}


谢谢
搜索更多相关主题的帖子: JAVA 小游戏 
2010-05-17 19:50
liubangchuan
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-17 22:48
jackyshen
Rank: 2
等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
收藏
得分:0 
唉,测验要用,没办法
2010-05-18 15:59
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:0 
程序代码:
import java.util.Scanner;

public class Grid {
    int maze[][]; // the 2D maze
    int width; // width of maze
    int height; // height of maze
    int hPos; // current horizontal position in maze
    int vPos; // current vertical position in maze

    /*
     * initializes maze using w (width) and h (height)
     */
    public Grid(int w, int h) {
        // set the width and height of maze
        width = w;
        height = h;

        // set the starting horizontal & vertical position in maze
        hPos = 0;
        vPos = 0;

        // create 2D maze of specified size
        maze = new int[height][width];

        visit(); // visit the current position
    }

    /*
     * increments the current position in maze by one
     * then displays the maze (note that if the value
     * reaches ten the value should reset back to zero
     */
    public void visit() {
        maze[hPos][vPos] += 1;
        if (maze[hPos][vPos] == 10)
            maze[hPos][vPos] = 0;
    }

    /*
     * print the maze values out as a 2 dimensional grid
     * with width x height dimensions
     * eg if the width is 4 and the height is 3 and only the top
     * left position has been visited, it should print the following:
     * 1000
     * 0000
     * 0000
     */
    public void display() {
        int i, j;
        for (i = 0; i < height; i++) {
            for (j = 0; j < width; j++) {
                System.out.print(maze[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    /*
     * move the current position in maze left by one position
     * needs to avoid going through left wall
     */
    public void left() {
        vPos -= 1;
        if (vPos < 0) {
            System.out.println("print left wall");
            vPos = 0;
        } else
            visit();
    }

    /*
     * move the current position in maze right by one position
     * needs to avoid going through right wall
     */
    public void right() {
        vPos += 1;
        if (vPos >= width) {
            System.out.println("print right wall");
            vPos = width - 1;
        } else
            visit();
    }

    /*
     * move the current position in maze up by one position
     * needs to avoid going through top wall
     */
    public void up() {
        hPos -= 1;
        if (hPos < 0) {
            System.out.println("print up wall");
            hPos = 0;
        } else
            visit();
    }

    /*
     * move the current position in maze down by one position
     * needs to avoid going through bottom wall
     */
    public void down() {
        hPos += 1;
        if (hPos >= height) {
            System.out.println("print down wall");
            hPos = height - 1;
        } else
            visit();
    }

    public static void main(String[] args) {
        Grid grid = new Grid(4, 4);
        // grid.visit();
        grid.display();
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        while (num != 0) {
            if (num == 4)
                grid.left();
            else if (num == 6)
                grid.right();
            else if (num == 5)
                grid.down();
            else if (num == 8)
                grid.up();
            else {
                System.out.println("wrong num");
                num = sc.nextInt();
                continue;
            }
            grid.display();
            num = sc.nextInt();
        }
    }
}
2010-05-19 14:26
jackyshen
Rank: 2
等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
收藏
得分:0 
谢谢帮助,可是如果按那些代表数字的方向键没有反应,其实还有其他的2个java,不知道有没有关系

import java.util.*;

public class Adder
{
  final int LEFT  = 4;
  final int RIGHT = 6;
  final int UP    = 8;
  final int DOWN  = 5;
  final int QUIT  = 0;
   /*
    *  the main method in Adder.java that is
    *  called from Program.java
    */
 
 // Scanner reader = new Scanner(System.in);

  public void run(int minDimension, int maxDimension)
  {
    int width;
    int height;
    int direction;

    System.out.println("Enter Width");
    width=getValidNum(minDimension, maxDimension);

    System.out.println("Enter Height");
    height=getValidNum(minDimension, maxDimension);

      // create grid with specific width and height
    Grid myGrid = new Grid(width, height);

      // display the grid
    myGrid.display();

    direction=getDirection();   // get the direction the user wants to move in

      // keep exploring grid until user wants to quit
    while(direction != QUIT)
    {
      if(direction == DOWN)
      {
        System.out.println("down");
        System.out.println("");
        myGrid.down();     // move down in grid
      }
      else if(direction == LEFT)
      {
        System.out.println("left");
        System.out.println("");
        myGrid.left();   // move left in grid
      }
      else if(direction == RIGHT)
      {
        System.out.println("right");
        System.out.println("");
        myGrid.right();  // move right in grid
      }
      else if(direction == UP)
      {
        System.out.println("up");
        System.out.println("");
        myGrid.up();     // move up in grid
      }
      direction=getDirection();   // get the next direction the user wants to move in
    }
    System.out.println("quit");
  }


   /*
    *  function to get a number within the range min-max (inclusive)
    *  eg getValidNum(1,3);
    *     will only accept 1,2 or 3
    *  must ensure that min is not larger than max
    *  if it is larger then the method must print an error
    *  and then quit the program by running:
    *  System.exit(0);
    */
  public int getValidNum(int min, int max)
  {
   
   
    int num=0;
   
   
    Scanner reader=new Scanner(System.in);
      num=reader.nextInt();
      
      if (min>max)
    {
         System.out.print("error");
         System.exit(0);
    }
      
      while(num<min||num>max)
    {
       System.out.println("error");
       System.out.println("Please re-enter the number");
       num=reader.nextInt();
    }
   
    return num;
  }


   /*
    *  function to get a number from the user representing a direction
    *  must only accept the numbers 4, 6, 8, 5 and 0
    */
  public int getDirection()
  {
    int num=0;
    if(num==1 || num==2 || num==3 || num==7 || num==9);
    System.out.println("directions: 4=left, 6=right, 8=up, 5=down, 0=quit");
    return num;
  }
}
2010-05-19 14:53
jackyshen
Rank: 2
等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
收藏
得分:0 
public class Program
{
  public static void main(String arg[])
  {
    int min=2;   // minimum grid dimension
    int max=10;  // maximum grid dimension
    Adder add = new Adder();
   
      // run main program
    add.run(min, max);
  }
}
2010-05-19 14:53
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:0 
程序代码:
import java.util.*;

public class Adder {
    final int LEFT = 4;
    final int RIGHT = 6;
    final int UP = 8;
    final int DOWN = 5;
    final int QUIT = 0;

    /*
     * the main method in Adder.java that is
     * called from Program.java
     */

    // Scanner reader = new Scanner(System.in);

    public void run(int minDimension, int maxDimension) {
        int width;
        int height;
        int direction;

        System.out.println("Enter Width");
        width = getValidNum(minDimension, maxDimension);

        System.out.println("Enter Height");
        height = getValidNum(minDimension, maxDimension);

        // create grid with specific width and height
        Grid myGrid = new Grid(width, height);

        // display the grid
//        myGrid.display();

        direction = getDirection(); // get the direction the user wants to move
                                    // in

        // keep exploring grid until user wants to quit
        while (direction != QUIT) {
            if (direction == DOWN) {
                System.out.println("down");
                System.out.println("");
                myGrid.down(); // move down in grid
            } else if (direction == LEFT) {
                System.out.println("left");
                System.out.println("");
                myGrid.left(); // move left in grid
            } else if (direction == RIGHT) {
                System.out.println("right");
                System.out.println("");
                myGrid.right(); // move right in grid
            } else if (direction == UP) {
                System.out.println("up");
                System.out.println("");
                myGrid.up(); // move up in grid
            }
            direction = getDirection(); // get the next direction the user wants
                                        // to move in
        }
        System.out.println("quit");
    }

    /*
     * function to get a number within the range min-max (inclusive)
     * eg getValidNum(1,3);
     * will only accept 1,2 or 3
     * must ensure that min is not larger than max
     * if it is larger then the method must print an error
     * and then quit the program by running:
     * System.exit(0);
     */
    public int getValidNum(int min, int max) {

        int num = 0;

        Scanner reader = new Scanner(System.in);
        num = reader.nextInt();

        if (min > max) {
            System.out.print("error");
            System.exit(0);
        }

        while (num < min || num > max) {
            System.out.println("error");
            System.out.println("Please re-enter the number");
            num = reader.nextInt();
        }

        return num;
    }

    /*
     * function to get a number from the user representing a direction
     * must only accept the numbers 4, 6, 8, 5 and 0
     */
    public int getDirection() {
        Scanner reader = new Scanner(System.in);
        int num = reader.nextInt();
        if (num != this.DOWN && num != this.UP && num != this.LEFT && num != this.RIGHT &&num!=this.QUIT)
            System.out.println("directions: 4=left, 6=right, 8=up, 5=down, 0=quit");
        return num;
    }
}import java.util.Scanner;

public class Grid {
    int maze[][]; // the 2D maze
    int width; // width of maze
    int height; // height of maze
    int hPos; // current horizontal position in maze
    int vPos; // current vertical position in maze

    /*
     * initializes maze using w (width) and h (height)
     */
    public Grid(int w, int h) {
        // set the width and height of maze
        width = w;
        height = h;

        // set the starting horizontal & vertical position in maze
        hPos = 0;
        vPos = 0;

        // create 2D maze of specified size
        maze = new int[height][width];

        visit(); // visit the current position
    }

    /*
     * increments the current position in maze by one
     * then displays the maze (note that if the value
     * reaches ten the value should reset back to zero
     */
    public void visit() {
        maze[hPos][vPos] += 1;
        if (maze[hPos][vPos] == 10)
            maze[hPos][vPos] = 0;
        display();
    }

    /*
     * print the maze values out as a 2 dimensional grid
     * with width x height dimensions
     * eg if the width is 4 and the height is 3 and only the top
     * left position has been visited, it should print the following:
     * 1000
     * 0000
     * 0000
     */
    public void display() {
        int i, j;
        for (i = 0; i < height; i++) {
            for (j = 0; j < width; j++) {
                System.out.print(maze[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    /*
     * move the current position in maze left by one position
     * needs to avoid going through left wall
     */
    public void left() {
        vPos -= 1;
        if (vPos < 0) {
            System.out.println("print left wall");
            vPos = 0;
        } else
            visit();
    }

    /*
     * move the current position in maze right by one position
     * needs to avoid going through right wall
     */
    public void right() {
        vPos += 1;
        if (vPos >= width) {
            System.out.println("print right wall");
            vPos = width - 1;
        } else
            visit();
    }

    /*
     * move the current position in maze up by one position
     * needs to avoid going through top wall
     */
    public void up() {
        hPos -= 1;
        if (hPos < 0) {
            System.out.println("print up wall");
            hPos = 0;
        } else
            visit();
    }

    /*
     * move the current position in maze down by one position
     * needs to avoid going through bottom wall
     */
    public void down() {
        hPos += 1;
        if (hPos >= height) {
            System.out.println("print down wall");
            hPos = height - 1;
        } else
            visit();
    }

    public static void main(String[] args) {
        Grid grid = new Grid(4, 4);
        // grid.visit();
        grid.display();
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        while (num != 0) {
            if (num == 4)
                grid.left();
            else if (num == 6)
                grid.right();
            else if (num == 5)
                grid.down();
            else if (num == 8)
                grid.up();
            else {
                System.out.println("wrong num");
                num = sc.nextInt();
                continue;
            }
            num = sc.nextInt();
        }
    }
}





2010-05-19 15:20
jackyshen
Rank: 2
等 级:论坛游民
帖 子:53
专家分:35
注 册:2010-5-16
收藏
得分:0 
就一个问题当长度和高度输好后,会print  2次  

10000
00000
00000
2010-05-19 15:36
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:20 
// display the grid
// myGrid.display();
这句不要就不会两次了。

public static void main(String[] args) {
        Grid grid = new Grid(4, 4);
        // grid.visit();
       //grid.display();这句也注释掉
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        while (num != 0) {
            if (num == 4)
                grid.left();
            else if (num == 6)
                grid.right();
            else if (num == 5)
                grid.down();
            else if (num == 8)
                grid.up();
            else {
                System.out.println("wrong num");
                num = sc.nextInt();
                continue;
            }
            num = sc.nextInt();
        }
    }
2010-05-19 15:54
快速回复:JAVA的一个小游戏的编程问题 (第2题)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021663 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved