翻转翻转翻转棋

只用if,loop,array的翻转翻转棋

之前在学校写的一个翻转棋程序,虽然看起来很简陋,还是值得纪念一下第一个”游戏“ 哈哈。人比较懒,我就直接粘贴我作业里写的内容了,重要的步骤会翻译下的。

在这里插入图片描述

  1. Game Description 游戏简介

At the start: there are four pieces on the board, two white and two black. You must try to capture opponent pieces and flip them over so they turn into your color.

Example: You have the black pieces. There is one black piece on the board; next to it are 4 white pieces in a line. You put a black piece at the end of the line, now you have 4 white pieces between 2 black pieces, so those white pieces turn to black.

翻转棋(黑白棋)的玩法:把夹击的棋子变成自己的颜色。

  1. Programming basics 用到的java编程知识

    非常适合初学者写来玩玩!因为真的只需要用到if,loop,array的知识就行了!

for loop:

for (*int i=0; i) {do something;}

if condition:

if (condition) {do something;}

while loop:

while (condition) {do something;}

Array: store variables

e.g. int []x : an array int[] [] x: 2D array

  1. Main logic 主要逻辑

-Use 1 to represent black pieces, 2 to represent white pieces.

用1来表示黑棋,2来表示白棋,0表示空位。

-A 2D array to simulate the board. Then, when we put integers in the array, we are putting pieces on the board.

用2D array来模仿棋盘

  int[][]a=new int[8][8];
  a[3][3]=1; a[3][4]=2;
  a[4][3]=2; a[4][4]=1;  //初始状态的棋子
   for (int r=0;r<a.length;r++)
      {for (int n=0;n<a[0].length;n++)    
       {System.out.print(a[r][n]+" ");}
      System.out.println();}    //打印出棋盘

-Use Scanner to get the players’ input.

用Scanner来得到棋手的落子位置,两人玩家分别的(x,y) 坐标。

     Scanner P = new Scanner(System.in);  Scanner Q = new Scanner(System.in);
     Scanner M = new Scanner(System.in);  Scanner N = new Scanner(System.in);

-Use while loops to alternative moves. First black, then white, and continue.

用while loop来轮流下棋,先是黑棋,再是白棋。

-Use while loops to check if the position in the array is occupied. If it is, then make the player enter a different position. Use if conditions to check if we can make that move to reverse pieces. If not, then make the player enter a different position.

用while loop来检验落子的位置是否已经有子,如果已经有棋子了,让玩家重新输入下棋位置。用if来检验下棋的位置是否可以翻转棋子,如果不行,则重新输入。

-Use for loops and if conditions to check if we reverse the pieces. Remember: as long as player 1’s pieces are between player 2’s pieces, the color of the pieces will change into that of player 2. In our simulation, we change the numbers. Change 1 to 2 or 2 to 1.

检验是否可以翻转棋子:

从每一横行,竖列,斜线来考虑。

-When checking the reverse condition, we need to consider from rows, columns, and diagonals.

比如,我们先检验横行中是否有翻转,

-Now check each row first! Get the position of the player’s input first. Check the input’s right side using a for loop & if condition: once we meet the same number, stop this check. Initialize the variable count, which counts how many positions are there between these two numbers. Then, initialize another variable count2 (for example), which counts how many opponents’ pieces are there between these two numbers. If count is equal to count2, then we change every number between them into the opponents’ numbers. (0 to 1 or 0 to 2).

首先获取玩家输入的位置。使用for循环和if条件来检测落子位置的右边横行:一旦我们遇到相同的数字,停止检查。初始化变量count,它数出在这两个相同数字之间多少个位置。然后,初始化另一个变量count2,数出这两个数字之间有多少个对手的棋子。如果count = count2,则将它们之间的每个数字都变成对方的数字

-Similar logic for the columns and diagonals.

对于竖列和斜线,用相似的逻辑来检查。

举个栗子,这是check column的code:

 int count = 0; int count2=0;
 for (int qq=-P1;qq<=7-P1;qq++) 
  {
       if (a[P1+qq][Q1]==1) 
     { 
         for (int aa=1;aa<=Math.abs(qq)-1;aa++)       
         {
          if(qq>=0&&a[P1+aa][Q1]==2){count++;} //上
          if(qq<0&&a[P1-aa][Q1]==2){count2++;} //下
         } //for loop to check the player2 counts
           
     if (count==(Math.abs(qq)-1)&&count!=0)
     {mm=1;
      for (int ii=1;ii<(Math.abs(qq)-1);ii++)
       {a[P1+ii][Q1]=1;} //上
     }
     if (count2==(Math.abs(qq)-1)&&count2!=0)
     {mm=1;
      for (int iii=1;iii<=(Math.abs(qq)-1);iii++)
       {a[P1-iii][Q1]=1;} //下
     }      
           
     }
       count=0; count2=0;
  } 

还有一些小细节需要注意,在这就不多赘述了。比如当黑棋没有棋可下时,这时候应该白棋继续;如果双方都无棋可下时,就直接开始数棋判断输赢。但只要把整体框架和“翻转”的这一步写好了,其它细节都可以慢慢添加。

这个code仅供参考,翻转棋小游戏还是很适合拿来练手的!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值