爲了學習J2EE的技術,現在在惡補java se的東西,先從最基礎的學起,教材選了李剛的那本《瘋狂java講義》
第一章是java語言概述,介紹了java語言的特點,java環境的搭建和一些java程序的基本原則。
第二章大概介紹了一下oop,這部分有uml語言介紹,我大略過了一遍,看不太懂(囧)。
第三章 數據類型和運算符,因爲我有點c的基礎,所以這章看的也很快。
第四章 流程控制和數組,這章講的非常詳細,把數組底層的存儲通過圖片介紹一下,但是我感覺還是似懂非懂的。
第四章的課後習題3.編寫一個控制檯的五子棋遊戲
我花了半天時間寫了一個,電腦的下法是隨機的。
import java.io.*;
public class Gobang
{
private String[][] board;
private static int BOARD_SIZE=15;
//棋盤初始化,大小設定爲15
private void initBoard()
{
board=new String[BOARD_SIZE][BOARD_SIZE];
for(int i=0;i<BOARD_SIZE;i++)
{
for(int j=0;j<BOARD_SIZE;j++)
{
board[i][j]="╋";
}
}
}
//打印棋盤
private void printBoard()
{
// for(int i=1;i<=BOARD_SIZE;i++)
// System.out.print("一");
// System.out.println();
for(int i=0;i<BOARD_SIZE;i++)
{
for(int j=0;j<BOARD_SIZE;j++)
{
System.out.print(board[i][j]);
}
System.out.println();
}
}
//判斷xpos,ypos處的子是否連成5個,是則返回ture,否返回false
public boolean isWin(int xpos,int ypos,String chesstype)
{
int sum=1;
int i;
int j;
//橫向判斷
i=xpos+1;
while(i>0&&i<BOARD_SIZE&&i<=xpos+4)
{
if(board[ypos-1][i-1]==chesstype)
sum++;
else
break;
i++;
}
i=xpos-1;
while(i>0&&i<BOARD_SIZE&&i>=xpos-4)
{
if(board[ypos-1][i-1]==chesstype)
sum++;
else
break;
i--;
}
if(sum==5)
return true;
sum=1;
//豎向判斷
i=ypos+1;
while(i>0&&i<BOARD_SIZE&&i<=ypos+4)
{
if(board[i-1][xpos-1]==chesstype)
sum++;
else
break;
i++;
}
i=ypos-1;
while(i>0&&i<BOARD_SIZE&&i>=ypos-4)
{
if(board[i-1][xpos-1]==chesstype)
sum++;
else
break;
i--;
}
if(sum==5)
return true;
sum=1;
//右斜判斷
i=xpos+1;
j=ypos+1;
while(i>0&&i<BOARD_SIZE&&i<=xpos+4&&j>0&&j<BOARD_SIZE&&j<=xpos+4)
{
if(board[j-1][i-1]==chesstype)
sum++;
else
break;
i++;
j++;
}
i=xpos-1;
j=ypos-1;
while(i>0&&i<BOARD_SIZE&&i>=xpos-4&&j>0&&j<BOARD_SIZE&&j>=xpos-4)
{
if(board[j-1][i-1]==chesstype)
sum++;
else
break;
i--;
j--;
}
if(sum==5)
return true;
sum=1;
//左斜判斷
i=xpos-1;
j=ypos+1;
while(i>0&&i<BOARD_SIZE&&i>=xpos-4&&j>0&&j<BOARD_SIZE&&j<=xpos+4)
{
if(board[j-1][i-1]==chesstype)
sum++;
else
break;
i--;
j++;
}
i=xpos+1;
j=ypos-1;
while(i>0&&i<BOARD_SIZE&&i>=xpos-4&&j>0&&j<BOARD_SIZE&&j<=xpos+4)
{
if(board[j-1][i-1]==chesstype)
sum++;
else
break;
i++;
j--;
}
if(sum==5)
return true;
return false;
}
public static void main(String[] args) throws Exception
{
Gobang gb=new Gobang();
gb.initBoard();
gb.printBoard();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String inputStr=null;
System.out.println("請輸入座標(x,y):");
while((inputStr=br.readLine())!=null)
{
String[] posStrArr=inputStr.split(",");
int xPos=Integer.parseInt(posStrArr[0]);
int yPos=Integer.parseInt(posStrArr[1]);
if(!(xPos>=0&&xPos<=BOARD_SIZE)||!(yPos>=0&&yPos<=BOARD_SIZE))
{
System.out.println("輸入不合法,請重新輸入座標:");
continue;
}
else if(gb.board[yPos-1][xPos-1]=="●"||gb.board[yPos-1][xPos-1]=="○")
{
System.out.println("不能重複下子,請重新輸入座標:");
continue;
}
else{
gb.board[yPos-1][xPos-1]="●";
}
int cxPos=(int)(Math.random()*BOARD_SIZE);
int cyPos=(int)(Math.random()*BOARD_SIZE);
// System.out.println(cxPos);
// System.out.println(cyPos);
while(gb.board[cyPos][cxPos]=="●"||gb.board[cyPos][cxPos]=="○")
{
cxPos=(int)(Math.random()*BOARD_SIZE);
cyPos=(int)(Math.random()*BOARD_SIZE);
}
gb.board[cyPos][cxPos]="○";
gb.printBoard();
if(gb.isWin(xPos,yPos,"●"))
{
System.out.println("You Win!");
break;
}
else if(gb.isWin(cxPos+1,cyPos+1,"○"))
{
System.out.println("Computer Win!");
break;
}
System.out.println("請輸入座標(x,y):");
}
}
}