经典五子棋(无图形化界面)的代码实现

import java.util.Scanner;
class Main{
    /*    
    1.创建一个棋盘    
    2.对棋盘进行初始化    
    3.打印棋盘    
    4.开始游戏
    */
    //1.    
    public static String[][] board=null;
    public static Scanner scanner=new Scanner(System.in);    
    public static void main(String[] args){        
    	//2.        
    	initBoard();        
    	//3.        
    	printBoard();        
    	//4.        
   	 startGame();    
    }
    public static void startGame(){        
    	int player=0;        
    	while(!isGameOver()){            
    		if(player%2==0){//黑方								
    		System.out.println(">>>黑方下棋:");
    		if(!xiaqi("O")){                    
    		continue;
    		}
    	}else{//白方                
    	System.out.println(">>>白方下棋:");                
    	if(!xiaqi("X")){                    
    	continue;                
    	}            
    }            
    player++;        
  }        
System.out.println(">>>游戏结束!!");  
}   
public static boolean isGameOver(){         
	for(int i=0;i<board.length;i++){
 	 	for(int j=0;j<board.length;j++){                
   	     	if(!board[i][j].equals("+")){               
   	     	//向右                    
    		if(j<11){                        
    			boolean flag=true;                        
    				for(int dy=1;dy<=4;dy++){                            
    			    		if(board[i][j]!=board[i][j+dy]){                                
    	    				flag=false;                                
    					break;                            
    					}                        
    				}                        
    				if(flag){                            
    			  	 return true;                        
    				}                    
    			}                    
    		//向下                    
    		if(i<11){                        
    		   boolean flag=true;                        
    		   for(int dx=1;dx<=4;dx++){                            
    			if(board[i][j]!=board[i+dx][j]){                                
    			   flag=false;                                
    			   break;                            
    			}                        
    		}                        
    		if(flag){                            
    		   return true;                        
    		}                    
    	}                    
    	//右下                    
    	if(i<11&&j<11){                        
    	   boolean flag=true;                        
    	   for(int d=1;d<=4;d++){                            
    		if(board[i][j]!=board[i+d][j+d]){                                
    		   flag=false;                                
    	           break;                            
    		}                       
    	 }                        
    	 if(flag){                            
    	    return true;                       
    	  }                    
    }                    
   		//右上                    
  		 if(i>3&&j<11){                        
      			boolean flag=true;                        
      			for(int d=1;d<=4;d++){                            			  
     				if(board[i][j]!=board[i-d][j+d]){                                
         				flag=false;                                
         				break;                            
         				}                        
     				}                        
    				if(flag){                            
     					return true;                        
     					}                    
    				 }                 
     			}            
     		}        
     	}        
     	return false;    
     }    
     public static boolean xiaqi(String chess){        
    	System.out.print(">>>请输入x坐标:");        
     	int x=scanner.nextInt()-1;        
     	System.out.print(">>>请输入y坐标:");        
     	int y=scanner.nextInt()-1;        
     	if(board[x][y].equals("+")){            
    		board[x][y]=chess;            
     		printBoard();            
    		return true;        
    		}else{            
     		System.out.println(">>>棋子已存在,请重新下棋!");            
     		return false;        
     		}    
     	}    
     public static void printBoard(){    //打印棋盘        
     	System.out.print("   ");        
     	for(int i=1;i<=15;i++){            
     		System.out.printf("%-3d",i);     //打印纵坐标,并让格式化输出        
     }        
     System.out.println();        
     for(int i=0;i<board.length;i++){            
     	System.out.printf("%-3d",i+1);    //打印横坐标,并进行格式化输出            
     	for(int j=0;j<board[i].length;j++){                    
    		 System.out.print(board[i][j]+"  ");   //将刚才已进行初始化填充的棋盘打印出来            
     	 }            
      	System.out.println();    //每打印一行棋,进行换行        
      	}    
      }    
      	public static void initBoard(){ //初始化棋盘        
      		board=new String[15][15];    //为二维型字符串数组开辟空间为15*15        
      		for(int i=0;i<board.length;i++){            
      			for(int j=0;j<board[i].length;j++){                
      				board[i][j]="+";    //棋盘的每一个坐标的初始化填充            
    			 }
		}    
 	}
 }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Windows API实现的C语言图形化界面五子棋游戏代码示例: ```c #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("FiveChess"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("Five Chess"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); // 在窗口中绘制出棋盘和棋子 EndPaint(hwnd, &ps); return 0; case WM_LBUTTONDOWN: // 处理用户下棋事件 return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } ``` 这段代码只是一个简单的框架,需要根据具体需求进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值