亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
1234下一頁
最近訪問板塊 發(fā)新帖
查看: 20608 | 回復(fù): 32
打印 上一主題 下一主題

shell game: Five stone game (五子棋) [復(fù)制鏈接]

論壇徽章:
2
射手座
日期:2014-10-10 15:59:4715-16賽季CBA聯(lián)賽之上海
日期:2016-03-03 10:27:14
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-08-11 08:17 |只看該作者 |倒序?yàn)g覽
本帖最后由 yinyuemi 于 2011-08-15 07:04 編輯

前段時間shell大賽的時候,寫了個兩人對弈的部分,后來人機(jī)對弈一直沒有時間去寫,這幾天閑下來了,把這部分也寫了下,也算是有個了結(jié)吧。程序編寫過程中,參考借鑒了很多人的思想和代碼,在此表示感謝。!
注:
1.游戲規(guī)則非常原始,沒有加標(biāo)準(zhǔn)規(guī)則。
2.兩人對弈部分,共同使用一套控制鍵操作棋子
3.人機(jī)對弈部分,人先手。 由于人機(jī)對弈部分,進(jìn)行了大量的運(yùn)算,消耗時間,請耐心等待(如果是cygwin環(huán)境,速度可能是個挑戰(zhàn)),另外計算機(jī)的"智商"比較低級,如果誰感興趣可以優(yōu)化算法,加以改進(jìn)。
歡迎大家測試,debug,拍磚。
  1. #!/bin/bash


  2. P1=6;P2=1
  3. P3=20;P4=1
  4. P5=14;P6=48


  5. function Chess_board ()
  6. {

  7. printf "\n\n\n";printf "%32s " " ";printf "\e[33m----->>FIVE STONE GAME<<-----\e[0m";printf "\n"
  8. printf "<Information:>\n"
  9. printf "%30s\e[43m                                 ";printf "\e[43m \e[0m\n"

  10. for i in {15..1}
  11. do
  12.         for j in {1..15}
  13.         do
  14.                 if [ $j = 1 ]
  15.                         then
  16.                         if [ $i -le 9 ]
  17.                                 then
  18.                                 printf "%30s" " ";echo -ne "\e[43m\e[30m"" "$i" "; printf "\e[43m\e[30m+ \e[0m"
  19.                         else
  20.                                 printf "%30s" " ";echo -ne "\e[43m\e[30m"$i" "; printf "\e[43m\e[30m+ \e[0m"
  21.                         fi
  22.                 elif [ $j = 15 ]
  23.                         then
  24.                         printf "\e[43m\e[30m+  \e[0m"
  25.                 elif [ $j = 8 ] && [ $i = 8 ]
  26.                         then
  27.                         printf "\e[43m\e[30m.\e[43m \e[0m"
  28.                 elif [ $j = 4 ] && [ $i = 4 ]
  29.                         then
  30.                         printf "\e[43m\e[30m.\e[43m \e[0m"
  31.                 elif [ $j = 12 ] && [ $i = 4 ]
  32.                         then
  33.                         printf "\e[43m\e[30m.\e[43m \e[0m"
  34.                 elif [ $j = 4 ] && [ $i = 12 ]
  35.                         then
  36.                         printf "\e[43m\e[30m.\e[43m \e[0m"
  37.                 elif [ $j = 12 ] && [ $i = 12 ]
  38.                         then
  39.                         printf "\e[43m\e[30m.\e[43m \e[0m"
  40.                 else
  41.                         printf "\e[43m\e[30m+ \e[0m"
  42.                 fi
  43.         done
  44.         printf "\n"
  45. done
  46. printf "%30s\e[43m\e[30m   A B C D E F G H I J K L M N O ";printf "\e[43m \e[0m\n"
  47. }

  48. function Location_cursor()
  49. {
  50.         h=$1;l=$2
  51.         printf "\e[$h;$l;H"
  52. }

  53. function Moving_cursor ()
  54. {
  55.         stty cbreak -echo
  56.         dd if=/dev/tty bs=1 count=1 2>/dev/null
  57.         stty -cbreak echo
  58. }

  59. function Choose ()
  60. {
  61.         x=0;y=0;
  62.         while true
  63.         do
  64.         case $(Moving_cursor) in
  65.         i|I)      printf "\e[1A";((x--));;  # Up
  66.         k|K)      printf "\e[1B";((x++));;  # Down
  67.         j|J)      printf "\e[2D";((y--));;  # Left
  68.         l|L)      printf "\e[2C";((y++));;  # Right
  69.                 " ")      break;; # Play
  70.                 b|B)          Start_game;; # Quit the game
  71.          esac
  72.         done
  73. }

  74. function Test_array ()
  75. {
  76.         point=$1;arr=$2
  77.         test=$(eval echo \${$arr[@]})
  78.         [[ $test == ${test/$point/} ]]
  79. }

  80. function y_n ()
  81. {
  82.         arr=$1
  83.         eval echo \${$arr[@]}|tr ' ' '\n'|sort -t',' -k1,1n -k2,2n|tr ',' ' '|Score $2 $3
  84. }

  85. function x_n (){
  86.         arr=$1
  87.         eval echo \${$arr[@]}|tr ' ' '\n' |awk -F, '{print $2" "$1}' |sort -k1n -k2n |Score $2 $3
  88. }

  89. function xy_l_n(){
  90.         arr=$1
  91.         eval echo \${$arr[@]}|tr ' ' '\n' |awk -F, '{print $1+$2" "$1}' |sort -k1n -k2n |Score $2 $3
  92. }

  93. function xy_r_n(){
  94.         arr=$1
  95.         eval echo \${$arr[@]}|tr ' ' '\n' |awk -F, '{print $1-$2" "$1}' |sort -k1n -k2n |Score $2 $3
  96. }

  97. function Score(){
  98.         num=$2;
  99.         if [ $1 == 1 ]
  100.         then
  101.                 awk -vn=$num '{if($1==v1){
  102.                         if($2==v2+1){a[$1]++}
  103.                         else{a[$1]=1}}
  104.                 else{a[$1]=1}
  105.                         v1=$1
  106.                         v2=$2}
  107.                 END{for(i in a)
  108.                         if(a[i]>=n){print "1";exit}
  109.                         print "0"}
  110.                 '
  111.         else
  112.                 awk -vn=$num '{if($1==v1){
  113.                         if($2==v2+1){a[$1]++}
  114.                         else{a[$1]=1}}
  115.                 else{a[$1]=1}
  116.                         v1=$1
  117.                         v2=$2}
  118.                 END{for(i in a){
  119.                                 if(a[i]>=n){print sum="1000000000";exit}
  120.                                 sum+=a[i]==4?"10000000":10*10^a[i]};
  121.                         print sum}
  122.                 '
  123.         fi
  124. }

  125. function Computer_decision(){
  126.         i=0
  127.         unset max_w
  128.         unset max_b
  129.         ((sum_w_old=$(xy_r_n array_white 0 5)+$(xy_l_n array_white 0 5)+$(x_n array_white 0 5)+$(y_n array_white 0 5)))
  130.         ((sum_b_old=$(xy_r_n array_black 0 5)+$(xy_l_n array_black 0 5)+$(x_n array_black 0 5)+$(y_n array_black 0 5)))
  131.         while((i<${#All_pos[@]}))
  132.         do
  133.                 black_point_="$black_point ${All_pos[$i]}"
  134.                 array_black_=(${black_point_:1})
  135.                 while_point_="$white_point ${All_pos[$i]}"
  136.                 array_white_=(${while_point_:1})
  137.                 ((sum_w=$(xy_r_n array_white_ 0 5)+$(xy_l_n array_white_ 0 5)+$(x_n array_white_ 0 5)+$(y_n array_white_ 0 5)))
  138.                 ((sum_b=$(xy_r_n array_black_ 0 5)+$(xy_l_n array_black_ 0 5)+$(x_n array_black_ 0 5)+$(y_n array_black_ 0 5)))
  139.                 ((increase_score_b=sum_b-sum_b_old))
  140.                 ((increase_score_w=sum_w-sum_w_old))
  141.                 if ((sum_w>=1000000000))||((sum_b>=1000000000))
  142.                 then
  143.                         echo ${All_pos[$i]}&&s=1&&break
  144.                 else
  145.                         if ((max_b>increase_score_b))
  146.                         then
  147.                                 ((max_b=max_b))
  148.                         else
  149.                                 ((max_b=increase_score_b,index_b=i))
  150.                         fi
  151.                
  152.                         if ((max_w>increase_score_w))
  153.                         then
  154.                                 ((max_w=max_w))
  155.                         else
  156.                                 ((max_w=increase_score_w,index_w=i))
  157.                         fi       
  158.                         s=0;
  159.                 fi
  160.                 ((i++))
  161.         done

  162.         if [ $s != 1 ]
  163.         then
  164.                 if ((max_b >= max_w))
  165.                 then
  166.                         echo ${All_pos[$index_b]}
  167.                 else
  168.                         echo ${All_pos[$index_w]}
  169.                 fi
  170.         fi
  171. }


  172. if_continue ()
  173. {       
  174.         Location_cursor 40 1
  175.         read -p "Do you want one more game?  Y/N: " answer1
  176.         case $answer1 in
  177.                 y|Y) clear;
  178.                         echo -e '\n\n\n\n\n\n      Do you want to continue player vs player\e[31m(1)\e[0m, or try player vs computer\e[33m(2)\e[0m?  '
  179.                         read -p "      Please choose:  " answer2;
  180.                         case $answer2 in
  181.                                 1) Main_program_player_vs_player ;;
  182.                                 2) Main_program_player_vs_computer ;;
  183.                         esac;;
  184.                 n|N) echo " Thanks!!! ";exit;;
  185.         esac
  186. }

  187. function Win_if ()
  188. {
  189.         who_=$1;
  190.         [ `xy_r_n $1 $2 $3` == 1 ]||[ `xy_l_n $1 $2 $3` == 1 ]||[ `x_n $1 $2 $3` == 1 ]||[ `y_n $1 $2 $3` == 1 ]&& Location_cursor 12 1 && echo "The ${who_##*_} win!!!" && Location_cursor $Cx $Cy && echo -e "\e[47m\e[30m+ \e[0m" && Location_cursor $Cx $Cy &&if_continue
  191. }

  192. function Main_program_player_vs_player ()
  193. {
  194.         clear # CygWin use echo -e "\e[2J"
  195.         Chess_board
  196.         Location_cursor $P5 $P6
  197.         ((Cx=P5,Cy=P6))
  198.         unset test_point step step_b step_w black_point white_point
  199.         array_white=(); array_black=()

  200.         while true
  201.         do
  202.                 Choose
  203.                 Location_cursor $P3 $P4
  204.                 echo -e "                          \n                           \n                            "
  205.                 ((Cx=Cx+x,Cy=2*y+Cy))
  206.                 ((abs_x=P5-Cx,abs_y=((Cy-P6))/2))
  207.                 test_point="$abs_x,$abs_y"
  208.        
  209.                 if (($abs_x <= 7)) && (($abs_x >= -7)) && (($abs_y <= 7)) && (($abs_y >= -7))
  210.                 then
  211.                         if ! Test_array $test_point array_white || ! Test_array $test_point array_black
  212.                         then
  213.                                 Location_cursor $P3 $P4       
  214.                                 echo -e "Not allowed\nPlease try again!!!" && test_result=1
  215.                         fi
  216.                 else
  217.                         Location_cursor $P3 $P4
  218.                         echo -e "Out of chess broad\nNot allowed\nPlease try again!!!" && test_result=1
  219.                 fi
  220.        
  221.                 if [ "$test_result" == 1 ]
  222.                 then
  223.                         ((Cx=P5-abs_x,Cy=2*abs_y+P6)) && test_result=0
  224.                         Location_cursor $Cx $Cy
  225.                 else
  226.                         ((step++))
  227.                         ((who_go=step%2))  
  228.                         Location_cursor $P1 $P2
  229.                         if [ $who_go = 1 ]
  230.                                 then
  231.                                 ((step_b++))
  232.                                 black_point="$black_point $abs_x,$abs_y"
  233.                                 array_black=(${black_point:1})
  234.                                 Win_if array_black 1 5
  235.                                 Location_cursor $P1 $P2
  236.                                 echo -e "\e[31m The Black \e[0m"
  237.                                 echo -e "\e[31mStep= $step_b\e[0m"  
  238.                                 echo "-->The White's turn<--"
  239.                                 echo "Step= $step_w"
  240.                                 Location_cursor $Cx $Cy
  241.                                 echo -e "\e[40m+ \e[0m"
  242.                                 Location_cursor $Cx $Cy
  243.                         else
  244.                                 ((step_w++))
  245.                                 white_point="$white_point $abs_x,$abs_y"  
  246.                                 array_white=(${white_point:1})
  247.                                 Win_if array_white 1 5
  248.                                 Location_cursor $P1 $P2               
  249.                                 echo -e "\e[31m The White \e[0m"
  250.                                 echo -e "\e[31mStep= $step_w\e[0m"  
  251.                                 echo  "-->The Black's turn<--"  
  252.                                 echo  "Step= $step_b"
  253.                                 Location_cursor $Cx $Cy
  254.                                 echo -e "\e[47m\e[30m+ \e[0m"
  255.                                 Location_cursor $Cx $Cy
  256.                         fi

  257.                 fi
  258.         done
  259. }

  260. #Main_program


  261. function Main_program_player_vs_computer ()
  262. {
  263.         clear # CygWin use echo -e "\e[2J"
  264.         All_pos=($(echo {-7..7},{-7..7}))
  265.         Chess_board
  266.         Location_cursor $P5 $P6
  267.         ((Cx=P5,Cy=P6))
  268.         unset test_point step step_b step_w black_point white_point
  269.         array_white=(); array_black=()
  270.        
  271.         while true
  272.         do
  273.                 Choose
  274.                 Location_cursor $P3 $P4
  275.                 echo -e "                          \n                           \n                            "
  276.                
  277.                 ((Cx=Cx+x,Cy=2*y+Cy))
  278.                 ((abs_x=P5-Cx,abs_y=((Cy-P6))/2))
  279.                 test_point="$abs_x,$abs_y"       
  280.                 if (($abs_x <= 7)) && (($abs_x >= -7)) && (($abs_y <= 7)) && (($abs_y >= -7))
  281.                 then
  282.                         if ! Test_array $test_point array_white
  283.                         then
  284.                                 Location_cursor $P3 $P4       
  285.                                 echo -e "Not allowed\nPlease try again!!!" && test_result=1
  286.                         fi
  287.                 else
  288.                         Location_cursor $P3 $P4
  289.                         echo -e "Out of chess broad\nNot allowed\nPlease try again!!!" && test_result=1
  290.                 fi

  291.        
  292.                 if [ "$test_result" == 1 ]
  293.                 then
  294.                         ((Cx=P5-abs_x,Cy=2*abs_y+P6)) && test_result=0
  295.                         Location_cursor $Cx $Cy
  296.                        
  297.                 else
  298.                         Location_cursor $P1 $P2
  299.                        
  300.                         ((step_b++))
  301.                         black_point="$black_point $abs_x,$abs_y"
  302.                         array_black=(${black_point:1})
  303.                         Location_cursor $((P3+2)) $P4
  304.                         Win_if array_black 1 5
  305.                        
  306.                         Location_cursor $P1 $P2
  307.                         echo -e "\e[31mThe Player \e[0m"
  308.                         echo -e "\e[31mStep= $step_b\e[0m"
  309.                         echo "-->The Computer's turn<--"
  310.                         echo "Step= $step_w"
  311.                         Location_cursor $Cx $Cy
  312.                         echo -e "\e[40m+ \e[0m"
  313.                         Location_cursor $Cx $Cy
  314.                         All_pos=(`echo ${All_pos[@]}|sed "s/ $abs_x,$abs_y / /"`)
  315.                        
  316.                 # computer:
  317.                         Comp_pos=$(Computer_decision)
  318.                         ((step_w++))
  319.                         white_point="$white_point $Comp_pos"
  320.                         array_white=(${white_point:1})
  321.                         abs_x=${Comp_pos%,*}
  322.                         abs_y=${Comp_pos#*,}
  323.                         ((Cx=P5-abs_x,Cy=2*abs_y+P6))
  324.                         Win_if array_white 1 5
  325.                         All_pos=(`echo ${All_pos[@]}|sed "s/ $Comp_pos / /"`)
  326.                         Location_cursor $P1 $P2
  327.                         echo -e "\e[31mThe Computer \e[0m"
  328.                         echo -e "\e[31mStep= $step_w\e[0m"
  329.                         echo -e "-->The Player's turn<--"
  330.                         echo -e "Step= $step_b"
  331.                         Location_cursor $Cx $Cy
  332.                         echo -e "\e[47m\e[30m+ \e[0m"
  333.                         Location_cursor $Cx $Cy
  334.                 fi
  335.         done
  336. }


  337. function Start_game()
  338. {
  339.         clear;  # CygWin use echo -e "\e[2J"
  340.         cat <<Mainpage
  341.        
  342.        
  343.                                   ----->>FIVE STONE GAME<<-----
  344.                                          <<How to play>>
  345.                         *****************************************************
  346.                         *   Game Types:                                            *
  347.                         *       (1) Main_program_player_vs_player           *
  348.                         *       (2) Main_program_player_vs_computer         *
  349.                         *   Control Keys:                                   *
  350.                         *       (i|I)Up                                            *
  351.                         *       (k|K)Down                                   *
  352.                         *       (j|J)Left                                   *
  353.                         *       (l|L)Right                                  *
  354.                         *       (space)Play                                 *
  355.                         *       (B|b)Back to the mainpage                   *
  356.                         *       (q|Q)Quit the game                          *
  357.                         *****************************************************
  358.                                                
  359.                                                
  360. Mainpage

  361.         read -p "  Please choose the game you want to play, or quit the game:   " answer;
  362.         case $answer in
  363.                 1) Main_program_player_vs_player ;;
  364.                 2) Main_program_player_vs_computer ;;
  365.                 q|Q) exit;;
  366.         esac
  367. }

  368. Start_game

復(fù)制代碼

f1.PNG (9.01 KB, 下載次數(shù): 84)

f1.PNG

f2.png (4.18 KB, 下載次數(shù): 103)

f2.png

評分

參與人數(shù) 2可用積分 +5 信譽(yù)積分 +10 收起 理由
bikkuri + 10 贊一個!
zooyo + 5 原創(chuàng)內(nèi)容

查看全部評分

論壇徽章:
1
摩羯座
日期:2014-12-29 15:59:36
2 [報告]
發(fā)表于 2011-08-11 08:19 |只看該作者

論壇徽章:
0
3 [報告]
發(fā)表于 2011-08-11 08:41 |只看該作者
這才是大拿,這才叫原創(chuàng),這就是SS……

論壇徽章:
3
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亞洲杯之阿曼
日期:2015-04-07 20:00:59
4 [報告]
發(fā)表于 2011-08-11 08:43 |只看該作者
拜拜大神...

論壇徽章:
0
5 [報告]
發(fā)表于 2011-08-11 08:43 |只看該作者
強(qiáng)人推出的產(chǎn)品,要頂個。

論壇徽章:
0
6 [報告]
發(fā)表于 2011-08-11 08:51 |只看該作者
最近流行寫游戲了啊,牛逼

論壇徽章:
0
7 [報告]
發(fā)表于 2011-08-11 08:53 |只看該作者
這個必須頂啊!

論壇徽章:
10
天蝎座
日期:2013-09-22 22:32:23程序設(shè)計版塊每日發(fā)帖之星
日期:2016-08-07 06:20:00lufei
日期:2016-06-17 17:38:40程序設(shè)計版塊每日發(fā)帖之星
日期:2016-06-12 06:20:002016科比退役紀(jì)念章
日期:2016-05-31 15:47:20CU十四周年紀(jì)念徽章
日期:2016-05-27 12:24:562015年亞洲杯之阿曼
日期:2015-05-03 21:01:352015年辭舊歲徽章
日期:2015-03-03 16:54:15天蝎座
日期:2013-10-20 21:05:24程序設(shè)計版塊每日發(fā)帖之星
日期:2016-08-11 06:20:00
8 [報告]
發(fā)表于 2011-08-11 09:03 |只看該作者
高手啊,收藏~~~

論壇徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-08-03 06:20:00數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-08-04 06:20:00
9 [報告]
發(fā)表于 2011-08-11 09:06 |只看該作者
LZ好樣的
慚愧,我出的題目,至今都沒時間好好寫一個出來

論壇徽章:
16
IT運(yùn)維版塊每日發(fā)帖之星
日期:2015-08-24 06:20:00綜合交流區(qū)版塊每日發(fā)帖之星
日期:2015-10-14 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2015-10-25 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2015-11-06 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-04-15 06:20:00IT運(yùn)維版塊每日發(fā)帖之星
日期:2016-05-21 06:20:00綜合交流區(qū)版塊每日發(fā)帖之星
日期:2016-08-16 06:20:002015七夕節(jié)徽章
日期:2015-08-21 11:06:17IT運(yùn)維版塊每日發(fā)帖之星
日期:2015-08-14 06:20:00
10 [報告]
發(fā)表于 2011-08-11 09:15 |只看該作者
不錯,厲害,我就好奇了,移動左右箭頭嗎?這個如何控制棋盤上的圖標(biāo)移動的
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP