1. 把二維數(shù)組當(dāng)作是一維的:
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、鄂州ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鄂州網(wǎng)站制作公司
例: 有數(shù)組char[3][3], 如 char[2][2]可以下標(biāo)對應(yīng)的數(shù)值為2×3 + 2 = 8,這個應(yīng)該是第三行第三列的數(shù)據(jù)下標(biāo)
2. 使用隨機數(shù)生成器生成一定范圍的下標(biāo)(例如,0~8),反算數(shù)組中的位置,這個位置作為下子的地方。(下子就是把對應(yīng)的數(shù)組中設(shè)成黑子或者白子)
3. 循環(huán)第二步,直至放滿
(注意,可能會產(chǎn)生重復(fù)下標(biāo),需要先判斷看看有沒有已經(jīng)有棋子在其上)
補充上邊的問題:
for(i=0; i8; i++)
{
getchar();
for(t=0; t8; t++)
{
ch = getchar();
if(ch=='1') chesscolor[i][t]=1;
else if(ch=='2') chesscolor[i][t]=2;
else if(ch=='0') chesscolor[i][t]=0;
}
}
int probability[8][8]={10,9,-1,0,0,-1,9,10,9,-10,0,0,0,-10,9,-1,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,-1,9,-10,0,0,0,-10,9,10,9,-1,0,0,-1,9,10,};
for(k=0;k8;k++)
for(l=0;l8;l++)
{
for(x=0;x8;x++)
for(y=0;y8;y++)
{
if(rules(x,y,chesscolor[x][y],chesscolor)==0)
{
xx[m]=x;
yy[n]=y;
m++;
n++;
}
}
for(m=0;m8;m++)
for(n=0;n8;n++)
{
if(probability[xx[m]][yy[n]]==10)
{
flag=1;
chesscolor[xx[m]][yy[n]]=rules(xx[m],yy[n],chesscolor[xx[m]][yy[n]],chesscolor);
}
else if(probability[xx[m]][yy[n]]==9)
{
if(probability[xx[m]--][yy[n]]==-1 || probability[xx[m]][yy[n]--]==-1)
{
flag=1;
chesscolor[xx[m]][yy[n]]=rules(xx[m],yy[n],chesscolor[xx[m]][yy[n]],chesscolor);
}
}
else if(probability[xx[m]][yy[n]]==-10)
{
flag=0;
continue;
}
}
if(flag=0)
{
r=rand()%7;
p=rand()%7;
chesscolor[xx[r]][yy[p]]=rules(xx[r],yy[p],chesscolor[r][p],chesscolor);
}
}
printf("讀入數(shù)據(jù):\n");
for(i=0; i8; i++)
{
for(t=0; t8; t++)
{
printf("%d", chesscolor[i][t]);
}
printf("\n");
}
}
上述代碼可以通過編譯,但運行時出錯,DEBUG顯示xx[m]=x;這行出錯,原因不知,求解,急?。。?!語言C語言
package com.test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;
public class GameDemo2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MainWindow mw=new MainWindow();
mw.setSize(400,400);
mw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mw.setVisible(true);
//mw.setResizable(false);
}
}
class MainWindow extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
NewPanel mp=null;
JButton jbt1;
JButton jbt2;
JButton jbt3;
JButton jbt4;
int flag=-1;
JPanel jp=null;
public MainWindow(){
//初始化組件
mp=new NewPanel();
mp.addMouseListener(mp);
jbt1=new JButton("O 先手");
jbt2=new JButton("X 先手 ");
jbt3=new JButton("RESET");
jbt4=new JButton("EXIT");
jp=new JPanel();
GridLayout gl=new GridLayout(4,1);
gl.setHgap(4);
gl.setVgap(5);
jp.setLayout(gl);
jp.add(jbt1);
jp.add(jbt2);
jp.add(jbt3);
jp.add(jbt4);
this.add(mp,BorderLayout.CENTER);
this.add(jp,BorderLayout.EAST);
jbt1.addActionListener(this);
jbt2.addActionListener(this);
jbt3.addActionListener(this);
jbt4.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jbt1){
flag=1;
mp.setHand(0);
jbt1.setEnabled(false);
jbt2.setEnabled(false);
}
if(e.getSource()==jbt2){
flag=2;
mp.setHand(1);
jbt1.setEnabled(false);
jbt2.setEnabled(false);
}
if(e.getSource()==jbt3){
flag=3;
mp.getGraphics().dispose();
this.remove(mp);
mp=new NewPanel();
this.add(mp,BorderLayout.CENTER);
mp.addMouseListener(mp);
this.remove(jp);
jp=new JPanel();
GridLayout gl=new GridLayout(4,1);
gl.setHgap(4);
gl.setVgap(5);
jp.setLayout(gl);
jp.add(jbt1);
jp.add(jbt2);
jp.add(jbt3);
jp.add(jbt4);
this.add(jp,BorderLayout.EAST);
this.validate();
jbt1.setEnabled(true);
jbt2.setEnabled(true);
}
if(e.getSource()==jbt4){
flag=4;
jbt1.setEnabled(false);
jbt2.setEnabled(false);
System.exit(0);
}
}
public int getFlag(){
return flag;
}
}
//畫板
class NewPanel extends JPanel implements MouseListener{
/**
*
*/
private static final long serialVersionUID = 1L;
//記錄畫圖的位置
int xx,yy;
//flag表示是否是初始化
int flag;
//0畫X,1畫0,2不畫
int kind;
//記錄是有有圖形的數(shù)組
//a[i]=0代表是空白可以畫 a[i]=1代表X a[i]=2代表畫O
int []a=null;
//記錄當(dāng)前鼠標(biāo)位置所在方格,便于判斷是否可以繼續(xù)畫
int curLoc;
//記錄誰先手 0-0 1-X
int firstHand;
int times;
//0-O 1-X
int whoWin;
public void setFlag(int f){
flag=f;
}
public void setHand(int h){
firstHand=h;
}
public void setWhoWin(){
whoWin=-1;
}
public NewPanel(){
xx=0;
yy=0;
flag=0;
a=new int[9];
kind=-1;
curLoc=0;
whoWin=-1;
firstHand=-1;
times=1;
whoWin=-1;
}
public void paint(Graphics g){
//super.paint(g);
g.drawRect(0, 0, 300, 300);
g.drawRect(0, 100, 300, 100);
g.drawRect(100, 0, 100, 300);
if(flag==1){
if(isFull()==false){
if(testWin()==false){
if((times+firstHand)%2==0){
if(a[curLoc]==0){
g.drawLine(xx,yy,xx+60,yy+60);
g.drawLine(xx+60, yy, xx, yy+60);
a[curLoc]=1;
times++;
if(testWin()==true){
System.out.println("X贏了");
}
}
}
if((times+firstHand)%2==1){
if(a[curLoc]==0){
g.drawOval(xx,yy,60,60);
a[curLoc]=2;
times++;
if(testWin()==true){
System.out.println("O贏了");
}
}
}
}
}else{
System.out.println("it is FULL!!!");
}
}
}
//將鼠標(biāo)的坐標(biāo)轉(zhuǎn)換為所畫圖形的基準(zhǔn)坐標(biāo) ,返回當(dāng)前鼠標(biāo)在記錄數(shù)組a中的下標(biāo)
//圓形為外接矩形的左上角
//X為左上角起始點的坐標(biāo)
public int Format(int x,int y,int kind){
//第一行第一列
int loc=-1;
if(x=0x100y=0y100){
xx=20;
yy=20;
loc= 0;
}
//第一行第二列
if(x=100x200y=0y100){
xx=120;
yy=20;
loc= 1;
}
//第一行第三列
if(x=200x300y=0y100){
xx=220;
yy=20;
loc= 2;
}
//第二行第一列
if(x=0x100y=100y200){
xx=20;
yy=120;
loc= 3;
}
//第二行第二列
if(x=100x200y=100y200){
xx=120;
yy=120;
loc= 4;
}
//第二行第三列
if(x=200x300y=100y200){
xx=220;
yy=120;
loc= 5;
}
//第三行第一列
if(x=0x100y=200y300){
xx=20;
yy=220;
loc= 6;
}
//第三行第二列
if(x=100x200y=200y300){
xx=120;
yy=220;
loc= 7;
}
//第三行第三列
if(x=200x300y=200y300){
xx=220;
yy=220;
loc= 8;
}
return loc;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
xx=e.getX();
yy=e.getY();
flag=1;
//kind=new Random().nextInt(3);
curLoc=Format(xx,yy,kind);
this.repaint();
}
//判斷是否可以繼續(xù)畫的函數(shù)
public boolean isFull(){
for(int i=0;i9;i++)
if(a[i]!=1a[i]!=2)
return false;
return true;
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public boolean testWin(){
boolean isWin=false;
//a[0]=1
if(a[0]!=0a[0]==a[1]a[0]==a[2]){
whoWin=a[0];
isWin=true;
}
if(a[0]!=0a[3]==a[0]a[0]==a[6]){
whoWin=a[0];
isWin=true;
}
if(a[0]!=0a[0]==a[4]a[0]==a[8]){
whoWin=a[0];
isWin=true;
}
if(a[1]!=0a[1]==a[4]a[1]==a[7]){
whoWin=a[1];
isWin=true;
}
if(a[2]!=0a[2]==a[4]a[2]==a[6]){
whoWin=a[2];
isWin=true;
}
if(a[2]!=0a[2]==a[5]a[2]==a[8]){
whoWin=a[2];
isWin=true;
}
if(a[3]!=0a[3]==a[4]a[3]==a[5]){
whoWin=a[3];
isWin=true;
}
if(a[6]!=0a[6]==a[7]a[6]==a[8]){
whoWin=a[6];
isWin=true;
}
return isWin;
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
//重新設(shè)置初始值
public void reset(){
this.getGraphics().dispose();
xx=0;
yy=0;
flag=0;
a=new int[9];
kind=-1;
curLoc=0;
whoWin=-1;
firstHand=-1;
times=1;
whoWin=-1;
}
}
public boolean fanqi(int x, int y) {
boolean a = false;
if (qi[x][y] == 0) {
if (flag) {// 黑吃白
if (y - 1 0 qi[x][y - 1] == -1) {
for (int i = 2; y - i = 0; i++) {
if (qi[x][y - i] == 1) {
for (int n = 1; n i; n++) {
qi[x][y - n] = 1;
}
a = true;
if (qi[x][y - i] == 0)
i = 9;
}
}
}
if (y + 1 8 qi[x][y + 1] == -1) {
for (int i = 2; y + i 8; i++) {
if (qi[x][y + i] == 1) {
for (int n = 1; n i; n++) {
qi[x][y + n] = 1;
}
a = true;
if (qi[x][y + i] == 0)
i = 9;
}
}
}
if (x + 1 8 qi[x + 1][y] == -1) {
for (int i = 2; x + i 8; i++) {
if (qi[x + i][y] == 1) {
for (int n = 1; n i; n++) {
qi[x + n][y] = 1;
}
a = true;
if (qi[x + i][y] == 0)
i = 9;
}
}
}
if (x - 1 = 0 qi[x - 1][y] == -1) {
for (int i = 2; x - i = 0; i++) {
if (qi[x - i][y] == 1) {
for (int n = 1; n i; n++) {
qi[x - n][y] = 1;
}
a = true;
if (qi[x - i][y] == 0)
i = 9;
}
}
}
if (x - 1 = 0 y - 1 = 0 qi[x - 1][y - 1] == -1) {
for (int i = 2; x - i = 0 y - i = 0; i++) {
if (qi[x - i][y - i] == 1) {
for (int n = 1; n i; n++) {
qi[x - n][y - n] = 1;
}
a = true;
if (qi[x - i][y - i] == 0)
i = 9;
}
}
}
if (x + 1 8 y - 1 = 0 qi[x + 1][y - 1] == -1) {
for (int i = 2; x + i 8 y - i = 0; i++) {
if (qi[x + i][y - i] == 1) {
for (int n = 1; n i; n++) {
qi[x + n][y - n] = 1;
}
a = true;
if (qi[x + i][y - i] == 0)
i = 9;
}
}
}
if (x - 1 = 0 y + 1 8 qi[x - 1][y + 1] == -1) {
for (int i = 2; x - i = 0 y + i 8; i++) {
if (qi[x - i][y + i] == 1) {
for (int n = 1; n i; n++) {
qi[x - n][y + n] = 1;
}
a = true;
if (qi[x - i][y + i] == 0)
i = 9;
}
}
}
if (x + 1 8 y + 1 8 qi[x + 1][y + 1] == -1) {
for (int i = 2; x + i 8 y + i 8; i++) {
if (qi[x + i][y + i] == 1) {
for (int n = 1; n i; n++) {
qi[x + n][y + n] = 1;
}
a = true;
if (qi[x + i][y + i] == 0)
i = 9;
}
}
}
}
else {// 白吃黑
if (y - 1 0 qi[x][y - 1] == 1) {
for (int i = 2; y - i = 0; i++) {
if (qi[x][y - i] == -1) {
for (int n = 1; n i; n++) {
qi[x][y - n] = -1;
}
a = true;
if (qi[x][y - i] == 0)
i = 9;
}
}
}
if (y + 1 8 qi[x][y + 1] == 1) {
for (int i = 2; y + i 8; i++) {
if (qi[x][y + i] == -1) {
for (int n = 1; n i; n++) {
qi[x][y + n] = -1;
}
a = true;
if (qi[x][y + i] == 0)
i = 9;
}
}
}
if (x + 1 8 qi[x + 1][y] == 1) {
for (int i = 2; x + i 8; i++) {
if (qi[x + i][y] == -1) {
for (int n = 1; n i; n++) {
qi[x + n][y] = -1;
}
a = true;
if (qi[x + i][y] == 0)
i = 9;
}
}
}
if (x - 1 = 0 qi[x - 1][y] == 1) {
for (int i = 2; x - i = 0; i++) {
if (qi[x - i][y] == -1) {
for (int n = 1; n i; n++) {
qi[x - n][y] = -1;
}
a = true;
if (qi[x - i][y] == 0)
i = 9;
}
}
}
if (x - 1 = 0 y - 1 = 0 qi[x - 1][y - 1] == 1) {
for (int i = 2; x - i = 0 y - i = 0; i++) {
if (qi[x - i][y - i] == -1) {
for (int n = 1; n i; n++) {
qi[x - n][y - n] = -1;
}
a = true;
if (qi[x - i][y - i] == 0)
i = 9;
}
}
}
if (x + 1 8 y - 1 = 0 qi[x + 1][y - 1] == 1) {
for (int i = 2; x + i 8 y - i = 0; i++) {
if (qi[x + i][y - i] == -1) {
for (int n = 1; n i; n++) {
qi[x + n][y - n] = -1;
}
a = true;
if (qi[x + i][y - i] == 0)
i = 9;
}
}
}
if (x - 1 = 0 y + 1 8 qi[x - 1][y + 1] == 1) {
for (int i = 2; x - i = 0 y + i 8; i++) {
if (qi[x - i][y + i] == -1) {
for (int n = 1; n i; n++) {
qi[x - n][y + n] = -1;
}
a = true;
if (qi[x - i][y + i] == 0)
i = 9;
}
}
}
if (x + 1 8 y + 1 8 qi[x + 1][y + 1] == 1) {
for (int i = 2; x + i 8 y + i 8; i++) {
if (qi[x + i][y + i] == -1) {
for (int n = 1; n i; n++) {
qi[x + n][y + n] = -1;
}
a = true;
if (qi[x + i][y + i] == 0)
i = 9;
}
}
}
}
}
if (a)
return true;
else
return false;
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import javax.swing.JOptionPane.*;
public class FiveChess
{
public static void main(String[] args)
{
JFrame myFrame = new JFrame("快樂五子棋");
myFrame.getContentPane().add(new MyPanel());
Toolkit mykit = myFrame.getToolkit();
Dimension mysize = mykit.getScreenSize();
myFrame.setBounds(0,0,mysize.width,mysize.height - 40);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.show();
}
}
class MyPanel extends JPanel implements MouseListener
{
private Point start = new Point(0,0);
private Point next = new Point(0,0);
boolean[][] chessBo = new boolean[19][19];
boolean chessCo = true; //判斷顏色的,true 黑
boolean winchess = true; //true,為勝利
MyPanel(){
addMouseListener(this);
setBackground(Color.blue);
}
public boolean winkill(int x,int y) //win ?
{
int countBlack = 0,countWhite = 0;
boolean winchess01 = false;
//x,y 取到r01,r02的值
for(int i = 0;i 5;i++)//判斷縱向下
{
if((x + i) 20)
break;
if(chessCo chessBo[x + i][y])
countBlack++;
else if(chessBo[x + i][y] (chessCo == false))
countWhite++;
}
for(int i = 0;i 5;i++)//判斷縱向上
{
if((x - i) = 0)
break;
if(chessCo chessBo[x - i][y])
countBlack++;
else if(chessBo[x - i][y] (chessCo == false))
countWhite++;
}
if((countBlack == 5) || (countWhite == 5))
winchess01 = true;
return winchess01;
}
public void paint(Graphics g)//畫棋盤
{
Graphics2D g2D = (Graphics2D)g;
g2D.setPaint(Color.black);
float pay = 60.0f,pbx = 60.0f;
float lett = 25.0f;
Point2D.Float p1 = new Point2D.Float(60.0f,pay);
Point2D.Float p2 = new Point2D.Float(510.0f,pay);
Point2D.Float p3 = new Point2D.Float(pbx,60.0f);
Point2D.Float p4 = new Point2D.Float(pbx,510.0f);
for(int i=0;i19;i++){
Line2D.Float lineH = new Line2D.Float(p1,p2);
Line2D.Float lineV = new Line2D.Float(p3,p4);
pay += lett;
p1 = new Point2D.Float(60.0f,pay);
p2 = new Point2D.Float(510.0f,pay);
pbx += lett;
p3 = new Point2D.Float(pbx,60.0f);
p4 = new Point2D.Float(pbx,510.0f);
g2D.draw(lineH);
g2D.draw(lineV);
}
}
public void mousePressed(MouseEvent evt)
{
}
public void mouseClicked(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
int clickCount = evt.getClickCount();
if(clickCount =1 ){
if((x 48 x 522) (y 48 y 522))
draw(x,y);
}
}
public void draw(int dx,int dy)
{
int r01 = 0,r02 = 0;
Graphics g = getGraphics();
start.x = dx;
start.y = dy;
r01 = (start.x + 13 - 60) / 25; //r01 、r02當(dāng)前的格子
r02 = (start.y + 13 - 60) / 25;
//System.out.println(r01 + "-" + r02);
next.x = 25 * r01 + 60 - 11;
next.y = 25 * r02 + 60 - 11;
//System.out.println(next.x + "-" + next.y);
if(chessCo){
chessCo = false;
g.setColor(Color.black);
g.fillOval(next.x,next.y,20,20);
chessBo[next.x][next.y] = true;//用這個時,黑白子可交替出現(xiàn),
//chessBo[r01][r02] = true;//用這個代替上面那個時黑白子不可交替了,不知道為什么會這樣
//加入判斷勝負(fù)的方法winkill()
if(winchess == winkill(r01,r02))
showMessage();
}
if(!chessCo){
chessCo = true;
g.setColor(Color.white);
g.fillOval(next.x,next.y,20,20);
chessBo[r01][r02] = true;
}
//g.drawOval(next.x,next.y,20,20);
g.dispose();
}
public void mouseReleased(MouseEvent evt)
{
}
public void mouseEntered(MouseEvent evt)
{
}
public void mouseExited(MouseEvent evt)
{
}
public void showMessage()
{
JOptionPane.showMessageDialog(null,
"You are win.",
"wide288 to Message!",
JOptionPane.INFORMATION_MESSAGE);
}
}