<div dir="ltr"><div>Un saluto a tutti.</div><div>Leggendo qualche 3rd sulla lista, ho preso spunto per un piccolo esercizio di codice.</div><div><br></div><div>Ho preso spunto da:</div><div><a href="http://en.wikipedia.org/wiki/Tic-tac-toe">http://en.wikipedia.org/wiki/Tic-tac-toe</a><br></div><div><a href="http://ostermiller.org/calc/tictactoe.html">http://ostermiller.org/calc/tictactoe.html</a><br></div><div><br></div><div>Consigli e suggerimenti sono ben accetti.</div><div>Come vedete ho implementato solo alcune delle regole ;-))</div><div><br></div><div># INIZIO SCRIPT *******************************************************</div><div>#!/usr/bin/python</div><div># coding: utf-8</div><div><br></div><div>from random import randint</div><div><br></div><div><br></div><div># SETUP ---------------------------------------------------------------------</div><div>player = ['X','O']  # giocatore</div><div>level = [2,1]       # 1=Novice,2=Intermediate,3=Experienced,4=Expert</div><div>diz_level={1:'NOVICE',2:'INTERMEDIATE',3:'EXPERIENCED',4:'EXPERT'}</div><div>blank = '.'         # carattere blank per griglia</div><div>cur_p =  0          # imposta current player (cur_p) e current level (cur_l)</div><div>cur_nr = 1          # contatore numero mossa</div><div>dim_b = 3           # imposta dimensione board (dim_b)</div><div># ---------------------------------------------------------------------------</div><div><br></div><div>def make_board(dim,val):</div><div>  # crea diz griglia di dimensioni dim con valore val</div><div>  diz = {}</div><div>  for x in range(dim):</div><div>    for y in range(dim):</div><div>      diz[x,y]=val</div><div>  return diz</div><div><br></div><div><br></div><div>def print_board(g,dim):</div><div>  # visualizza griglia g di dimensione dim</div><div>  for y in range(dim):</div><div>    for x in range(dim):</div><div>      print g[x,y],</div><div>    print</div><div><br></div><div><br></div><div>def celle_libere(g,dim,blank):</div><div>  ris = []</div><div>  for y in range(dim):</div><div>    for x in range(dim):</div><div>      if g[x,y] == blank:</div><div>        ris.append((x,y))</div><div>  return ris</div><div><br></div><div><br></div><div>def best_move(g,dim_b,p,nr,blank):</div><div>  # sceglie una possibile posizione (x,y)</div><div>  # per il giocatore p di livello l alla mossa nr</div><div>  celle = celle_libere(g,dim_b,blank)</div><div>  dim = len(celle)</div><div>  if dim:</div><div>    if level[p] == 1:  # Livello NOVICE</div><div>      state = 'RANDOM'</div><div>      i = randint(0,dim-1)  # sceglie random cella da quelle libere</div><div>      x = celle[i][0]</div><div>      y = celle[i][1]</div><div>      return x,y,state</div><div><br></div><div>    if level[p] == 2:  # Livello INTERMEDIATE</div><div>      for (x,y) in celle:  </div><div>           # 1) Win: If the player has two in a row, they can place a third to get three in a row.</div><div>           if check_win(g,dim_b,dim_b-1,x,y,p):</div><div>             state = 'WIN'</div><div>             return x,y,state</div><div>           # 2) Block: If the opponent has two in a row, the player must play the third themselves to block the opponent.</div><div>           elif check_win(g,dim_b,dim_b-1,x,y,not(p)):</div><div>             state = 'BLOCK'</div><div>             return x,y,state</div><div>      state = 'RANDOM'</div><div>      i = randint(0,dim-1)  # sceglie random cella da quelle libere</div><div>      x = celle[i][0]</div><div>      y = celle[i][1]</div><div>      return x,y,state</div><div><br></div><div>    if level[p] == 4:</div><div>      if nr == 1: return 1,1 # prima mossa CENTER</div><div>      #if nr == 2: return -1,-1 #cella non valida</div><div>      for (x,y) in celle:  </div><div>           # 1) Win: If the player has two in a row, they can place a third to get three in a row.</div><div>           if check_win(g,dim_b,dim_b-1,x,y,p): return x,y,state</div><div><br></div><div>           # 2) Block: If the opponent has two in a row, the player must play the third themselves to block the opponent.</div><div>           elif check_win(g,dim_b,dim_b-1,x,y,not(p)): return x,y,state</div><div><br></div><div>           # 3) Fork: Create an opportunity where the player has two threats to win (two non-blocked lines of 2).</div><div><br></div><div>           # 4) Blocking an opponent's fork:</div><div>           # Option 1: The player should create two in a row to force the opponent into defending, as long as it doesn't result in them creating a fork.</div><div>           # For example,  if "X" has a corner, "O" has the center, and "X" has the opposite corner as well,</div><div>           #                 "O" must not play a corner in order to win. (Playing a corner in this scenario creates a fork for "X" to win.) </div><div>           # Option 2: If there is a configuration where the opponent can fork, the player should block that fork.</div><div><br></div><div>           # 5) Center: A player marks the center.</div><div>           #   (If it is the first move of the game, playing on a corner gives "O" more opportunities to make a mistake and may therefore be the better choice;</div><div>           #    however, it makes no difference between perfect players.)</div><div><br></div><div>           # 6) Opposite corner: If the opponent is in the corner, the player plays the opposite corner.</div><div><br></div><div>           # 7) Empty corner: The player plays in a corner square.</div><div><br></div><div>           # 8) Empty side: The player plays in a middle square on any of the 4 sides.</div><div><br></div><div><br></div><div><br></div><div><br></div><div>  else:</div><div>    return -1,-1,state # non ci sono celle libere quindi imposto coordinate per pareggio</div><div><br></div><div><br></div><div>def check_win(g,dim,dim_win,x,y,p):</div><div>  # controlla se ho dim elementi di player p</div><div>  # in riga, colonna, diagonali</div><div>  col=row=diag=rdiag=0</div><div>  ris=False</div><div>  for i in range(dim):</div><div>    if g[x,i]==player[p]: col += 1</div><div>    if g[i,y]==player[p]: row += 1</div><div>    if g[i,i]==player[p]: diag += 1</div><div>    if g[i,dim-i-1]==player[p]: rdiag +=1</div><div>  if row==dim_win or col==dim_win or diag==dim_win or rdiag==dim_win: ris=True</div><div>  return ris</div><div><br></div><div><br></div><div><br></div><div>if __name__ == '__main__':</div><div>  </div><div><br></div><div>  board = make_board(dim_b,blank)</div><div><br></div><div>  while True:</div><div>  </div><div>    x,y,state = best_move(board,dim_b,cur_p,cur_nr,blank)</div><div>    print 'nr_mossa: '+ str(cur_nr),'cur_p: ' + player[cur_p],'cur_l: ' + str(level[cur_p])+':'+diz_level[level[cur_p]],'state: '+state</div><div><br></div><div>    if x == y == -1: # se non ci sono celle vuote</div><div>      print 'PAREGGIO'  </div><div>      break</div><div>    else:  </div><div>      board[x,y] = player[cur_p] # metto il simbolo di cur_p in posizione x,y</div><div>      cur_nr += 1                # incremento il numero della mossa</div><div>      print_board(board,dim_b)   # visualizzo la griglia</div><div><br></div><div>      if check_win(board,dim_b,dim_b,x,y,cur_p): #controlla se cur_p ha vinto</div><div>        print 'VITTORIA DI ' + player[cur_p]</div><div>        break</div><div>      cur_p = not cur_p  # seleziono l'altro giocatore</div><div>      print '-'*20   </div><div><br></div><div># FINE SCRIPT *******************************************************<br></div></div>