日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java 井字棋 人机_一个井字棋tictactoe游戏的java实现 | Soo Smart!

發(fā)布時(shí)間:2025/3/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 井字棋 人机_一个井字棋tictactoe游戏的java实现 | Soo Smart! 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這是一個(gè)井字棋游戲的java實(shí)現(xiàn)。摘錄于stackoverflow。

游戲規(guī)則很簡(jiǎn)單,只要一方棋子在水平線,垂直線或者對(duì)角線任意一條線上排列成功即為獲勝。

作者原先的代碼存在著一些問題:

代碼如下:

一共有幾個(gè)類: play, player, human, computer, set

Play類:

import java.util.Scanner;

public class play {

public static void main(String[] args) {

System.out.println("Welcome to Tickle Tackle Toe!!! :D");

System.out.println();

//creat markers

String marker1 = "x";

String marker2 = "o";

boolean playAgain = true;

Scanner s = new Scanner(System.in);

//create player objects

Human human = new Human();

Computer computer = new Computer();

while(playAgain){

//run board setup

set Setup = new set();

Setup.createBoard();

Setup.printBoard();

System.out.println("please choose your marker");

System.out.println("type 1 for 'x' or 2 for 'o'");

//set markers

if(s.nextInt() == 1){

// create player objects

human.setMarker("x");

computer.setMarker("o");

}

else

{

human.setMarker("o");

computer.setMarker("x");

}

// determine who goes first

int first = (int) (Math.random() * 2);

boolean won = false;

int turns = 0;

if(first == 0){

System.out.println("You go first!");

System.out.println();

while(!won){

human.takeTurn(Setup.getBoard());

turns++;

Setup.printBoard();

if(Setup.hasWon(Setup.getBoard())){

won = true;

System.out.println("Congrats you won!");

}

if(turns == 9){

won = true;

System.out.println("Its a draw!");

break;

}

if(!won){

computer.takeTurn(Setup.getBoard(), human);

turns++;

System.out.println();

Setup.printBoard();

System.out.println();

if(Setup.hasWon(Setup.getBoard())){

won = true;

System.out.println("You lost!");

}

if(turns == 9){

won = true;

System.out.println("Its a draw!");

break;

}

}

} // close while 1

}

else {

System.out.println("Computer goes first!");

System.out.println();

while(!won){

computer.takeTurn(Setup.getBoard(), human);

turns++;

Setup.printBoard();

if(Setup.hasWon(Setup.getBoard())){

won = true;

System.out.println("You lost!");

}

if(!won){

human.takeTurn(Setup.getBoard());

turns++;

System.out.println();

Setup.printBoard();

System.out.println();

if(Setup.hasWon(Setup.getBoard())){

won = true;

System.out.println("Congrats you won!");

}

}

} // close while 2

}

System.out.println("Would you like to play again? Type 1 for yes or 2 to quit");

System.out.println();

if(s.nextInt() == 2){

playAgain = false;

}

}

}

}

Player類

public class player {

public String Marker;

// set marker method

public void setMarker(String marker){

Marker = marker;

}

//get marker method

public String getMarker(){

return Marker;

}

}

棋局Set類:

import java.util.Random;

import java.util.Scanner;

public class set {

// setup variables default board size and board

private int N = 3;

public String[][] board = new String [N][N];

public boolean hasWon (String [] [] board){

//horizontal

for(int i = 0; i<3; i++){

if(board[i][0].equals(board[i][1]) && board[i][1].equals(board[i][2])){

return true;

}

}

//vertical

for(int i = 0; i<3; i++){

if(board [0][i].equals(board[1][i]) && board[1][i].equals(board[2][i])){

return true;

}

}

//diagonal

if(board[0][0].equals(board[1][1]) && board[1][1].equals(board[2][2]) || board[2][0].equals(board[1][1]) && board[1][1].equals(board[0][2]))

return true;

return false;

}

int x = 1;

public void createBoard(){

for(int i = 0; i < board.length; i++){

for(int j = 0; j < board.length; j++){

board[i][j] = "" + (x);

x++;

}

}

}

public void printBoard(){

for(int i = 0; i < board.length; i++){

for(int j = 0; j < board.length; j++){

System.out.print("[" + board[i][j] + "]" + " ");

}

System.out.println();

}

}

public String[][] getBoard(){

return board;

}

}

Computer類

class Computer extends player {

public Computer(){

}

int boardsize = 3;

public void takeTurn(String[][] board, Human human) {

int vertical = 0;

int horizontal = 0;

int diagonal = 0;

boolean mademove = false;

// check if you can take a win horizontally

for(int i = 0; i<3; i++){

if(board[0][i].equals(board[1][i]) && board[0][i].equals(Marker)){

if(board[2][i] != human.getMarker()){

board[2][i] = Marker;

mademove = true;

return;

}

}

}

for(int i = 0; i<3; i++){

if(board[2][i].equals(board[1][i]) && board[2][i].equals(Marker)){

if(board[0][i] != human.getMarker()){

board[0][i] = Marker;

mademove = true;

return;

}

}

}

// check if you can take a win vertically

for(int i = 0; i<3; i++){

if(board[i][0].equals(board[i][1]) && board[i][0].equals(Marker)){

if(board[i][2] != human.getMarker()){

board[i][2] = Marker;

mademove = true;

return;

}

}

}

for(int i = 0; i<3; i++){

if(board[i][2].equals(board[i][1]) && board[i][2].equals(Marker)){

if(board[i][0] != human.getMarker()){

board[i][0] = Marker;

mademove = true;

return;

}

}

}

// check if you can take a win diagonally

if(board[0][0].equals(board[1][1]) && board[0][0].equals(Marker)){

if(board[2][2] != human.getMarker()){

board[2][2] = Marker;

mademove = true;

return;

}

}

if(board[2][2].equals(board[1][1]) && board[2][2].equals(Marker)){

if(board[0][0] != human.getMarker()){

board[0][0] = Marker;

mademove = true;

return;

}

}

if(board[0][0].equals(board[1][1]) && board[0][0].equals(Marker)){

if(board[2][2] != human.getMarker()){

board[2][2] = Marker;

mademove = true;

return;

}

}

if(board[0][2].equals(board[1][1]) && board[0][2].equals(Marker)){

if(board[2][0] != human.getMarker()){

board[2][0] = Marker;

mademove = true;

return;

}

}

if(board[2][0].equals(board[1][1]) && board[2][0].equals(Marker)){

if(board[0][2] != human.getMarker()){

board[0][2] = Marker;

mademove = true;

return;

}

}

// BLOCKS!!!! //

// check if you can block a win horizontally

for(int i = 0; i<3; i++){

if(board[0][i].equals(board[1][i]) && board[0][i].equals(human.getMarker())){

if(board[2][i] != Marker){

board[2][i] = Marker;

mademove = true;

return;

}

}

}

for(int i = 0; i<3; i++){

if(board[2][i].equals(board[1][i]) && board[0][i].equals(human.getMarker())){

if(board[0][i] != Marker){

board[0][i] = Marker;

mademove = true;

return;

}

}

}

// check if you can block a win horizontally

for(int i = 0; i<3; i++){

if(board[i][0].equals(board[i][1]) && board[i][0].equals(human.getMarker())){

if(board[i][2] != Marker){

board[i][2] = Marker;

mademove = true;

return;

}

}

}

for(int i = 0; i<3; i++){

if(board[i][2].equals(board[i][1]) && board[i][2].equals(human.getMarker())){

if(board[i][0] != Marker){

board[i][0] = Marker;

mademove = true;

return;

}

}

}

// check if you can block a win diagonally

if(board[0][0].equals(board[1][1]) && board[0][0].equals(human.getMarker())){

if(board[2][2] != Marker){

board[2][2] = Marker;

mademove = true;

return;

}

}

if(board[2][2].equals(board[1][1]) && board[2][2].equals(human.getMarker())){

if(board[0][0] != Marker){

board[0][0] = Marker;

mademove = true;

return;

}

}

if(board[0][0].equals(board[1][1]) && board[0][0].equals(human.getMarker())){

board[2][2] = Marker;

mademove = true;

return;

}

if(board[0][2].equals(board[1][1]) && board[0][2].equals(human.getMarker())){

if(board[2][0] != Marker){

board[2][0] = Marker;

mademove = true;

return;

}

}

if(board[2][0].equals(board[1][1]) && board[2][0].equals(human.getMarker())){

if(board[0][2] != Marker){

board[0][2] = Marker;

mademove = true;

return;

}

}

// make random move if above rules dont apply

for(int i = 0; i<3; i++){

if(board[i][0] != "x" && board[i][0] != "o"){

board[i][0] = Marker;

return;

}

}

for(int i = 0; i<3; i++){

if(board[i][1] != "x" && board[i][0] != "o"){

board[i][1] = Marker;

return;

}

}

for(int i = 0; i<3; i++){

if(board[i][2] != "x" && board[i][0] != "o"){

board[i][2] = Marker;

return;

}

}

}

}

Human類

import java.util.Scanner;

class Human extends player {

public Human(){

}

public void takeTurn(String[][] board) {

Scanner s = new Scanner(System.in);

boolean turn = true;

while(turn){

System.out.println("please enter row");

int row = s.nextInt();

System.out.println("please enter col");

int col = s.nextInt();

System.out.print("you entered "+row+" "+col);

System.out.println();

if(board[row - 1][col - 1] != "x" && board[row - 1][col - 1] != "o"){

board[row - 1][col - 1] = Marker;

turn = false;

} // closes if

else {

System.out.println("Already Marker here! please guess again!");

}

} // ends while

} // ends method

} // ends class

這段代碼需要進(jìn)一步調(diào)試。摘錄此僅供參考。

原文出處:https://stackoverflow.com/questions/10645381/tictactoe-ai-java

總結(jié)

以上是生活随笔為你收集整理的java 井字棋 人机_一个井字棋tictactoe游戏的java实现 | Soo Smart!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。