题目1
1.创建5个线程对象
线程名设置为(Thread01,Thread02,Thread03,Thread04,Thread05)使用 代码实现5个线程有序的循环打印,效果如下:
Thread01正在打印1
Thread02正在打印2
Thread03正在打印3
Thread04正在打印4
Thread05正在打印5
Thread01正在打印6
Thread02正在打印7
…
package Test;
public class Test01 {
public static void main(String[] args) {
Object lock = new Object();
int[] currentNum = {1}; // 记录当前应该打印的数字
for (int i = 1; i <= 5; i++) {
final int threadNum = i;
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
try {
for (int j = 0; j < 10; j++) {
// 等待轮到自己打印
while ((currentNum[0] - 1) % 5 != threadNum - 1) {
lock.wait();
}
// 打印数字
System.out.println("Thread" + threadNum + "正在打印" + currentNum[0]);
currentNum[0]++;
// 唤醒下一个线程
lock.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
// 开始第一个线程的执行
synchronized (lock) {
lock.notifyAll();
}
}
}
运行截图
题目二
package Test;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server {
public static void main(String[] args) throws IOException{
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
Scanner scan = new Scanner(System.in);
Gobang gobang = new Gobang(); // 创建五子棋对象
StatusCode statusCode = new StatusCode(); // 添加状态码
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
PrintStream ps = new PrintStream(socket.getOutputStream());
while (true){
// ps.println("服务端:" + scan.next());
System.out.println("请服务端输入落子位置:");
int x = scan.nextInt() - 1;
int y = scan.nextInt() - 1;
// 落子
if(gobang.play(x, y, true) != statusCode.successfulLanding) {
continue;
}
// ps.println("服务端端落子:(" + x + "," + y + ")");
ps.println(x + "," + y);
String str = br.readLine();
System.out.println(str);
int client_x = Integer.parseInt(str.split(",")[0]);
int client_y = Integer.parseInt(str.split(",")[1]);
System.out.println("客户端x:" + client_x);
System.out.println("客户端y:" + client_y);
if(gobang.play(client_x , client_y, false) != statusCode.successfulLanding) {
continue;
}
// 打印棋盘
for (String[] ss : gobang.gobang) {
for (String element : ss) {
System.out.print(element);
}
System.out.println();
}
// 提升作业:判断输赢
if(gobang.isWin(x, y)) {
System.out.println("服务端胜利");
break;
}
}
}
}
package Test;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 8080);
Scanner scan = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
PrintStream ps = new PrintStream(socket.getOutputStream());
Gobang gobang = new Gobang(); // 创建五子棋对象
StatusCode statusCode = new StatusCode(); // 添加状态码
while(true){
System.out.println("请客户端输入落子位置:");
int x = scan.nextInt() - 1;
int y = scan.nextInt() - 1;
// 落子
if(gobang.play(x, y, false) != statusCode.successfulLanding) {
continue;
}
// ps.println("客户端落子:(" + x + "," + y + ")");
ps.println(x + "," + y);
String str = br.readLine();
System.out.println(str);
int server_x = Integer.parseInt(str.split(",")[0]);
int server_y = Integer.parseInt(str.split(",")[1]);
System.out.println("服务端x:" + server_x);
System.out.println("服务端y:" + server_y);
if(gobang.play(server_x, server_y, true) != statusCode.successfulLanding) {
continue;
}
// 打印棋盘
for (String[] ss : gobang.gobang) {
for (String element : ss) {
System.out.print(element);
}
System.out.println();
}
if(gobang.isWin(x, y)) {
System.out.println("客户端胜利");
break;
}
}
}
}
package Test;
public class Gobang {
/**
* 五子棋类
* */
// 棋盘长度
int len = 20;
// 棋盘容器
String[][] gobang = new String[len][len];
// 棋盘的符号
String add = "┼";// ┼
String black = "●";
String white = "○";
String[] indexs = { "⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖", "⒗", "⒘", "⒙",
"⒚", "⒛" };
public Gobang(){
init();
printGoBang();
}
// 初始化棋盘
private void init() {
for (int i = 0; i < gobang.length; i++) {
for (int j = 0; j < gobang[i].length; j++) {
if (j==len-1) { // 每行的最后一列 设置 xx行
gobang[i][j] = indexs[i] + "行";
}else if(i==len-1){ // 每列的最后一行 设置 xx 列
gobang[i][j] = indexs[j];
}else {
gobang[i][j] = add;
}
}
}
}
// 打印棋盘
private void printGoBang() {
for (int i = 0; i < gobang.length; i++) {
for (int j = 0; j < gobang[i].length; j++) {
System.out.print(gobang[i][j]);
}
System.out.println();
}
}
// 判断坐标是否在棋盘内
private boolean isIndexOutOfGoBang(int x, int y) {
if(x<0 || x>len-2 || y<0 || y>len-2){
System.out.println("落子失败 - 坐标超出棋盘范围");
return true;
}
return false;
}
// 判断坐标是否有棋子
private boolean isPiece(int x, int y) {
if(!gobang[x][y].equals(add)){
return true;
}
return false;
}
// 落子
public int play(int x, int y, boolean bool) {
StatusCode statusCode = new StatusCode();
// 判断坐标是否超出棋盘范围
if (isIndexOutOfGoBang(x, y)) {
System.out.println("落子失败 - 坐标超出棋盘范围");
return statusCode.indexOutOfRange;
}
// 判断坐标上是否有棋子
if (isPiece(x, y)) {
System.out.println("落子失败 - 坐标上已有棋子");
return statusCode.existingPieces;
}
gobang[x][y] = (bool) ? black : white;
return statusCode.successfulLanding;
}
public boolean isWin(int x, int y) {
if(
upAndDown(x, y)
|| leftAndRight(x, y)
|| leftUpAndRightDown(x, y)
|| leftDownAndRightUp(x, y)
) {
return true;
}
return false;
}
// 落子之后, 上下判断输赢
private boolean upAndDown(int x, int y) {
int count = 1;
for (int i = x-1;i>=0;i--) {
if (this.gobang[x][y]==this.gobang[i][y]) {
count++;
}else {
break;
}
}
for(int i=x+1;i<=this.len;i++) {
if(this.gobang[x][y]==this.gobang[i][y]) {
count++;
}else {
break;
}
}
return (count>=5)?true:false;
}
// 落子之后, 左右判断输赢
private boolean leftAndRight(int x, int y) {
int count = 1;
for (int i=y-1;i>=0;i--) {
if(this.gobang[x][y]==this.gobang[x][i]) {
count++;
}else {
break;
}
}
for (int i=y+1;i<=this.len;i++) {
if(this.gobang[x][y]==this.gobang[x][i]) {
count++;
}else {
break;
}
}
return (count>=5)?true:false;
}
// 落子之后, 左上右下判断输赢
private boolean leftUpAndRightDown(int x, int y) {
int count = 1;
for (int i=y-1, j=x-1;i>=0&&j>=0;i--, j--) {
if(this.gobang[x][y]==this.gobang[j][i]) {
count++;
}else {
break;
}
}
for (int i=y+1, j=x+1;i<=this.len&&j<=this.len;i++,j++) {
if(this.gobang[x][y]==this.gobang[j][i]) {
count++;
}else {
break;
}
}
return (count>=5)?true:false;
}
// 落子之后, 左下右上判断输赢
private boolean leftDownAndRightUp(int x, int y) {
int count = 1;
for (int i=y-1, j=x+1;i>=0&&j<=this.len;i--, j++) {
if(this.gobang[x][y]==this.gobang[j][i]) {
count++;
}else {
break;
}
}
for (int i=y+1, j=x-1;i<=this.len&&j>=0;i++,j--) {
if(this.gobang[x][y]==this.gobang[j][i]) {
count++;
}else {
break;
}
}
return (count>=5)?true:false;
}
}
package Test;
public class StatusCode {
/**
* 五子棋状态码
* */
int indexOutOfRange = -1; // 超出棋盘范围
int existingPieces = -2; // 该位置已有棋子
int successfulLanding = 1; // 落子成功
public int getIndexOutOfRange() {
return this.indexOutOfRange;
}
public int getExistingPieces() {
return this.existingPieces;
}
public int getSuccessfulLanding() {
return this.successfulLanding;
}
}
运行截图
存在bug
- 使用单聊模式为通信方式, 如果一方能正常落子, 一方错误输入已经落过子的坐标, 正常落子方会比错误落子方多落一子.
- 一方胜利之后, 另外一方的窗口不能结束.
- 双方第一次落子如果落在一处, 没有相应的处理逻辑.
- 落子双方没有先后顺序, 正常先后顺序应该为i黑先白后
- …