啓動main函數控制檯輸入棋子座標
-----------------歡迎進入五子棋界面-----------------
1 雙人對戰
5 退 出
請選擇(1-5):
1
----------歡迎進入棋盤頁面----------
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請黑子輸入棋子, 如輸入格式是座標:3-1
5-5
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請白子輸入棋子, 如輸入格式是座標:3-1
4-5
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 2 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請黑子輸入棋子, 如輸入格式是座標:3-1
5-6
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 2 0 0 0 0 0
0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請白子輸入棋子, 如輸入格式是座標:3-1
5-4
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 2 0 0 0 0 0
0 0 0 0 2 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請黑子輸入棋子, 如輸入格式是座標:3-1
4-3
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 2 0 0 0 0 0
0 0 0 0 2 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請白子輸入棋子, 如輸入格式是座標:3-1
6-4
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 2 0 0 0 0 0
0 0 0 0 2 1 1 0 0 0 0
0 0 0 0 2 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請黑子輸入棋子, 如輸入格式是座標:3-1
5-2
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 2 0 0 0 0 0
0 0 1 0 2 1 1 0 0 0 0
0 0 0 0 2 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請白子輸入棋子, 如輸入格式是座標:3-1
6-3
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 2 0 0 0 0 0
0 0 1 0 2 1 1 0 0 0 0
0 0 0 2 2 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請黑子輸入棋子, 如輸入格式是座標:3-1
6-1
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 2 0 0 0 0 0
0 0 1 0 2 1 1 0 0 0 0
0 1 0 2 2 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請白子輸入棋子, 如輸入格式是座標:3-1
7-2
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 2 0 0 0 0 0
0 0 1 0 2 1 1 0 0 0 0
0 1 0 2 2 0 0 0 0 0 0
0 0 2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請黑子輸入棋子, 如輸入格式是座標:3-1
6-0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 2 0 0 0 0 0
0 0 1 0 2 1 1 0 0 0 0
1 1 0 2 2 0 0 0 0 0 0
0 0 2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
請白子輸入棋子, 如輸入格式是座標:3-1
8-1
恭喜棋手:2 贏了
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 2 0 0 0 0 0
0 0 1 0 2 1 1 0 0 0 0
1 1 0 2 2 0 0 0 0 0 0
0 0 2 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
-----------------歡迎進入五子棋界面-----------------
1 雙人對戰
5 退 出
請選擇(1-5):
代碼實現
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn
import scala.util.control.Breaks.{break, breakable}
/**
* 五子棋方案二:每落下一子,判斷其 橫-豎-左斜-右斜 是否有一個方向五子相連
* 優點:1.不需要對所有棋子遍歷,只需關注落下的棋子
* 缺點:1.判斷條件相對複雜
*/
object WuziqiDemo02 {
//棋盤大小
val rowSize = 11
val colSize = 11
//定義一個循環變量,控制是否退出 while
var loop = true
var lp2 = true
//定義一個 key 用於接收用戶輸入的選項
var key = ' '
var index = ""
var indexType = 1 //
def main(args: Array[String]): Unit = {
val array = Array.ofDim[Int](rowSize, colSize)
do {
println("-----------------歡迎進入五子棋界面----------------- ")
println(" 1 雙人對戰")
println(" 5 退 出")
println("請選擇(1-5):")
key = StdIn.readChar()
key match {
case '1' => {
println("----------歡迎進入棋盤頁面----------")
println("")
showQipan(array)
println("")
while(lp2){
if(indexType == 1){
println("請黑子輸入棋子, 如輸入格式是座標:3-1")
}else{
println("請白子輸入棋子, 如輸入格式是座標:3-1")
}
index =StdIn.readLine()//接收棋子座標
val ints = index.split("-").map(_.toInt)
//判斷棋子座標格式
if(ints.size == 2){
val row = ints(0)
val col = ints(1)
//判斷座標位置
if(row > rowSize -1 || col > colSize -1){
println("棋子座標超出棋盤,輸入無效!!!")
}else{
array(row)(col) = indexType
if(luoziSY(array,new Node(row,col,indexType))){
lp2 = false
println("恭喜棋手:"+indexType +" 贏了")
}
if(indexType == 1){
indexType = 2
}else{
indexType=1
}
showQipan(array)
}
}else{
println("輸入座標有誤!!!")
}
}
}
case '5' => this.loop = false
case _ => println("輸入無法識別")
}
}while(loop)
println("你退出了五子棋系統...")
}
//落子判斷輸贏
def luoziSY(array: Array[Array[Int]], node: Node): Boolean = {
//判斷棋子四個方向
if(dealHeng(array,node) || dealShu(array,node) || dealZuoxie(array,node) || dealYouxie(array,node))
true
else
false
}
def dealHeng(array: Array[Array[Int]], node: Node): Boolean = {
var count: Int = 0;
//1. 當前棋子向左,最多遍歷4個位子即可
breakable {
for (i <- 1 to 4) {
//到左列邊界了
if (node.col - i < 0) {
break()
}
//橫向向左發現不同棋子則結束
if (array(node.row)(node.col - i) != node.value) {
break()
}
count += 1 //左橫移動,同子則加
}
}
//2.向右判斷
breakable {
for (i <- 0 to 4) { //0 表示當前子
//到右列邊界了
if (node.col + i > colSize - 1) {
break()
}
//橫向向右發現不同棋子則結束
if (array(node.row)(node.col + i) != node.value) {
break()
}
count += 1 //右橫移動,同子則加
}
}
if (count >= 5) true else false
}
def dealShu(array: Array[Array[Int]], node: Node): Boolean = {
var count: Int = 0;
//1. 當前棋子縱向 向上尋找相同棋子
breakable {
for (i <- 1 to 4) {
//小於0 則超出起始行邊界
if (node.row - i < 0) {
break()
}
//縱向向上發現不同棋子則結束
if (array(node.row - i)(node.col) != node.value) {
break()
}
count += 1 //縱上移動,同子則加
}
}
//2. 當前棋子縱向 向下尋找相同棋子
breakable {
for (i <- 0 to 4) { //0 表示當前棋子所在行
//大於rowSize - 1 則超出最後一行邊界
if (node.row + i > rowSize - 1) {
break()
}
//當前行縱向向下發現不同棋子則結束
if (array(node.row - i)(node.col) != node.value) {
break()
}
count += 1 //縱上移動,同子則加
}
}
if (count >= 5) true else false
}
//下端在左,上端在右的斜線
def dealZuoxie(array: Array[Array[Int]], node: Node): Boolean = {
var count: Int = 0;
//1. 當前棋子左斜-棋子下部分 向下尋找相同棋子
breakable {
for (i <- 1 to 4) {
//超出最高行或者小於最小列則判斷下個棋子超出界限,則終止循環
if (node.row + i > rowSize - 1 || node.col - i < 0) {
break()
}
//左斜向下,發現不同棋子則結束
if (array(node.row + i)(node.col - i) != node.value) {
break()
}
count += 1 //縱上移動,同子則加
}
}
//2. 左斜上半部分判斷
breakable {
for (i <- 0 to 4) {
//超出最低行或者大於最大列則判斷下個棋子超出界限,則終止循環
if (node.row - i < 0 || node.col + i > colSize - 1) {
break()
}
//左斜向上,發現不同棋子則結束
if (array(node.row - i)(node.col + i) != node.value) {
break()
}
count += 1 //縱上移動,同子則加
}
}
if (count >= 5) true else false
}
//下端在右,上端在左的斜線
def dealYouxie(array: Array[Array[Int]], node: Node): Boolean = {
var count: Int = 0;
//1. 當前棋子右斜-棋子下部分 向下尋找相同棋子
breakable {
for (i <- 1 to 4) {
//超出最高行或者超出最大列,則判斷下個棋子超出界限,則終止循環
if (node.row + i > rowSize - 1 || node.col + i > colSize - 1) {
break()
}
//左斜向下,發現不同棋子則結束
if (array(node.row + i)(node.col + i) != node.value) {
break()
}
count += 1 //縱上移動,同子則加
}
}
//2. 右斜上半部分判斷
breakable {
for (i <- 0 to 4) {
//超出最低行或者大於最大列則判斷下個棋子超出界限,則終止循環
if (node.row - i < 0 || node.col - i < 0) {
break()
}
//右斜向上,發現不同棋子則結束
if (array(node.row - i)(node.col - i) != node.value) {
break()
}
count += 1 //縱上移動,同子則加
}
}
if (count >= 5) true else false
}
//棋盤顯示函數
def showQipan(array: Array[Array[Int]]): Unit = {
for (items <- array) {
for (i <- items) {
print(i + "\t")
}
println()
}
}
}
功能擴展:這裏是控制檯輸出,不是界面話,就沒有做過多功能
1、對局暫存,可以使用稀疏數組實現數據存盤
2、悔棋,根據落下棋子使用數據棧來完成,悔一步棋出棧一個數據即可