Liberty BASIC Community Forum
« AI game : not 3 »

Welcome Guest. Please Login or Register.
Aug 16th, 2017, 4:49pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: AI game : not 3  (Read 141 times)
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 902
xx AI game : not 3
« Thread started on: Jul 29th, 2017, 07:51am »

this is a old idea from my i first coded it on the c64

object of the game :
put your X so that you nmy putsw the 3e X
in a row or colorm

i got version mouse 1 working [ i think ]
if i got the right code for the REM's
we wil have version 2
this one wil learn from ALL the moves made
and wil get smater every time it plays

Code:
WindowWidth = 600
WindowHeight = 650
global who.play , who1 , who2 , human , rando , ai , tel
global game,hr,rh,rr,hai,aih,bordstate
human = 1
rando = 2
ai = 3
hr = 1
rh = 2
rr = 3
hai = 4
aih = 5
dim bord( 9 ) , borden( 9 ) , z( 9 ) , memory$( 512 )
nomainwin
button #m.1 , "1" , btn.click , UL , 0 , 400 , 200 , 200
button #m.2 , "2" , btn.click , UL , 200 , 400 , 200 , 200
button #m.3 , "3" , btn.click , UL , 400 , 400 , 200 , 200
button #m.4 , "4" , btn.click , UL , 0 , 200 , 200 , 200
button #m.5 , "5" , btn.click , UL , 200 , 200 , 200 , 200
button #m.6 , "6" , btn.click , UL , 400 , 200 , 200 , 200
button #m.7 , "7" , btn.click , UL , 0 , 0 , 200 , 200
button #m.8 , "8" , btn.click , UL , 200 , 0 , 200 , 200
button #m.9 , "9" , btn.click , UL , 400 , 0 , 200 , 200
menu #m , "Game" _
        , "You vs rando" , [hr] _
        , "Rando vs you" , [rh] _
        , | , "Quit" , [quit]

open "NOT 3" for window as #m
  #m "trapclose [quit]"
  #m "font 70 bold"
  ''  call load.memory
wait
[quit]
  close #m
end
[hr]
  game = hr
  who1 = human
  who2 = rando
  call new.game
wait
[rh]
  game = rh
  who1 = rando
  who2 = human
  call new.game
wait
[rr]
  game = rr
  who1 = rando
  who2 = rando
  call new.game
wait
[hai]
  game = hai
  who1 = human
  who2 = ai
  call new.game
wait
[aih]
  game = aih
  who1 = ai
  who2 = human
  call new.game
wait
sub new.game
  for i = 1 to 9
    bord( i ) = 0
  next i
  bord( 0 ) = 1
  #m.1 , "1"
  #m.2 , "2"
  #m.3 , "3"
  #m.4 , "4"
  #m.5 , "5"
  #m.6 , "6"
  #m.7 , "7"
  #m.8 , "8"
  #m.9 , "9"
  tel = 0
  who.play = who1
  bordstate = 0
  if who.play = rando then
    call rando.move
  end if
''  if who.play = ai then
''    call ai.move
''  end if
end sub
[new.memory]
  for mem = 0 to 511
    q$ = ""
    for i = 0 to 8
      if mem and 2 ^ i then
        for t = 0 to 4
          q$ = q$ + str$( i + 1 )
        next t
      end if
    next i
    memory$( mem ) = q$
  next mem
  ''call save.memory
wait
''sub save.memory
''  open "not-3.txt" for output as #uit
''    for i = 0 to 511
''      #uit memory$( i )
''    next i
''  close #uit
''end sub
''sub load.memory
''  open "not-3.txt" for input as #in
''    for i = 0 to 511
''      get #in , q$
''      memory$(i) = q$
''    next i
''  close #in
''end sub
sub btn.click h$
  if who.play = human then
    z = val( right$( h$ , 1 ) )
    if bord( z ) then
      notice "ON A EMPTY PLASE PLEASE !!"
      exit sub
    end if
    call any.move z
  end if
  if game = hr or game = rh then
    call rando.move
  end if
''  if game hai or game = aih then
''    call ai.move
''  end if
end sub
sub any.move z
  bord( z ) = 1
  z( tel ) = z
  bordstate = bordstate or 2 ^ z
  borden( tel ) = bordstate
  tel = tel + 1
  select case z
    case 1
      #m.1 , "X"
    case 2
      #m.2 , "X"
    case 3
      #m.3 , "X"
    case 4
      #m.4 , "X"
    case 5
      #m.5 , "X"
    case 6
      #m.6 , "X"
    case 7
      #m.7 , "X"
    case 8
      #m.8 , "X"
    case 9
      #m.9 , "X"
    case else
  end select
  if who.play = who1 then
    who.play = who2
  else
    who.play = who1
  end if
  if row() and game <> rr then
    select case who.play
      case human
        notice "You won !!"
      case rando
        notice "rando has won ."
      case else '' ai
        notice "AI has won ."
    end select
    goto [quit]
  end if
  ''call leren
end sub
function ry( a , b , c )
  ry = bord(a) and bord(b) and bord(c)
end function
function row()
  row = ry( 1 , 2 , 3 ) _
     or ry( 4 , 5 , 6 ) _
     or ry( 7 , 8 , 9 ) _
     or ry( 1 , 4 , 7 ) _
     or ry( 2 , 5 , 8 ) _
     or ry( 3 , 6 , 9 )
end function
sub rando.move
  dice = int( rnd(0) * 9 )
  while bord( dice )
    dice = int( rnd(0) * 9 )
  wend
  if game <> rr then
    notice "Rando moves on : " ; dice
  end if
  call any.move dice
end sub
sub ai.move
  q$ = memory$( bordstate )
  p = int( rnd(0) * ( len( q$ ) - 1 ) )
  z = val( mid$( q$ , p+1 , 1 ) )
  notice "AI moves on : " ; z
  call any.move z
end sub
sub leren
  select case tel
    case 3
      call kill.move borden( 2 ) , z( 2 )
    case 4
      call add.move borden( 2 ) , z( 2 )
      call kill.move borden( 3 ) , z( 3 )
    case 5
      call forget.move borden( 2 ) , z( 2 )
      call add.move borden( 3 ) , z( 3 )
      call kill.move borden( 4 ) , z( 4 )
    case else
  end select
end sub
sub forget.move b , z
  p = instr( memory$( b ) , str$( z ) )
  if p = 0 then exit sub
  l$ = left$( memory$( b ) , p )
  r$ = right$( memory$( b ) , len( memory$( b ) ) - p )
  memory$( b ) = l$ + r$
end sub
sub add.move b , z
  if len( memory$( b ) ) < 200 then
    memory$( b ) = memory$( b ) + str$( z )
  end if
end sub
sub kill.move b , z
  while instr( memory$( b ) , str$( z ) )
    call forget.move b , z
  wend
end sub
 
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 902
xx Re: AI game : not 3
« Reply #1 on: Aug 1st, 2017, 05:36am »

update :
trying to get keys working

i need help whit sub load.memory
and sub save.memory

until then we have version 1

error :
#m when error

Code:
WindowWidth = 600
WindowHeight = 650
global who.play , who1 , who2 , human , rando , ai , tel
global game,hr,rh,rr,hai,aih,bordstate,times.played
human = 1
rando = 2
ai = 3
hr = 1
rh = 2
rr = 3
hai = 4
aih = 5
dim bord( 9 ) , borden( 9 ) , z( 9 ) , memory$( 512 )
nomainwin
button #m.1 , "1" , btn.click , UL , 0 , 400 , 200 , 200
button #m.2 , "2" , btn.click , UL , 200 , 400 , 200 , 200
button #m.3 , "3" , btn.click , UL , 400 , 400 , 200 , 200
button #m.4 , "4" , btn.click , UL , 0 , 200 , 200 , 200
button #m.5 , "5" , btn.click , UL , 200 , 200 , 200 , 200
button #m.6 , "6" , btn.click , UL , 400 , 200 , 200 , 200
button #m.7 , "7" , btn.click , UL , 0 , 0 , 200 , 200
button #m.8 , "8" , btn.click , UL , 200 , 0 , 200 , 200
button #m.9 , "9" , btn.click , UL , 400 , 0 , 200 , 200
menu #m , "Game" _
        , "You vs rando" , [hr] _
        , "Rando vs you" , [rh] _
        , | , "Quit" , [quit]
''todo : add :
''  learing [rr]
''  Human vs AI [hai]
''  AI vs Human [aih]

menu #m , "?" _
        , "about not 3" , [help]

open "NOT 3" for window as #m
  #m "trapclose [quit]"
  #m "font 100 bold"
  #m "when characterInput [key]"
  #m "setfocus"
  ''  call load.memory
wait
[quit]
  close #m
end
[hr]
  game = hr
  who1 = human
  who2 = rando
  call new.game
wait
[rh]
  game = rh
  who1 = rando
  who2 = human
  call new.game
wait
[rr]
  game = rr
  who1 = rando
  who2 = rando
  prompt "how many times ?" ; q$
  for i = times.played to times.played + val( q$ )
    call new.game
    while not( row() )
      call rando.move
    wend
  next i
wait
[hai]
  game = hai
  who1 = human
  who2 = ai
  call new.game
wait
[aih]
  game = aih
  who1 = ai
  who2 = human
  call new.game
wait
[help]
  notice "This game was created by bluatigro ." + chr$( 13 ) _
       + "Object of the game :" + chr$( 13 ) _
       + "Move your X so that the computer" + chr$( 13 ) _
       + "moves the 3e X in a row or colom ." + chr$( 13 ) _
       + chr$( 13 ) ; times.played ; " Times played ."
wait
sub new.game
  for i = 1 to 9
    bord( i ) = 0
  next i
  bord( 0 ) = 1
  #m.1 , "1"
  #m.2 , "2"
  #m.3 , "3"
  #m.4 , "4"
  #m.5 , "5"
  #m.6 , "6"
  #m.7 , "7"
  #m.8 , "8"
  #m.9 , "9"
  tel = 0
  who.play = who1
  bordstate = 0
  if who.play = rando then
    call rando.move
  end if
''  if who.play = ai then
''    call ai.move
''  end if
end sub
[new.memory]
  times.played = 0
  for mem = 0 to 511
    q$ = ""
    for i = 0 to 8
      if mem and 2 ^ i then
        for t = 0 to 4
          q$ = q$ + str$( i + 1 )
        next t
      end if
    next i
    memory$( mem ) = q$
  next mem
  ''call save.memory
wait
''sub save.memory
''  open "not-3.txt" for output as #uit
''    #uit times.played
''    for i = 0 to 511
''      #uit memory$( i )
''    next i
''  close #uit
''end sub
''sub load.memory
''  open "not-3.txt" for input as #in
''    get #in , times.played
''    for i = 0 to 511
''      get #in , q$
''      memory$(i) = q$
''    next i
''  close #in
''end sub
[key]
  key$ = right$( Inkey$ , 1 )
  if instr( "123456789" , key$ ) then
    call btn.click key$
  end if
wait
sub btn.click h$
  if who.play = human then
    z = val( right$( h$ , 1 ) )
    if bord( z ) then
      notice "ON A EMPTY PLASE PLEASE !!"
      exit sub
    end if
    call any.move z
  end if
  if game = hr or game = rh then
    call rando.move
  end if
''  if game hai or game = aih then
''    call ai.move
''  end if
end sub
sub any.move z
  bord( z ) = 1
  z( tel ) = z
  bordstate = bordstate or 2 ^ z
  borden( tel ) = bordstate
  tel = tel + 1
  select case z
    case 1
      #m.1 , "X"
    case 2
      #m.2 , "X"
    case 3
      #m.3 , "X"
    case 4
      #m.4 , "X"
    case 5
      #m.5 , "X"
    case 6
      #m.6 , "X"
    case 7
      #m.7 , "X"
    case 8
      #m.8 , "X"
    case 9
      #m.9 , "X"
    case else
  end select
  if who.play = who1 then
    who.play = who2
  else
    who.play = who1
  end if
  if row() and game <> rr then
    select case who.play
      case human
        notice "You won !!"
      case rando
        notice "rando has won ."
      case else '' ai
        notice "AI has won ."
    end select
  end if
  times.played = times.played + 1
  ''call leren
end sub
function ry( a , b , c )
  ry = bord(a) and bord(b) and bord(c)
end function
function row()
  row = ry( 1 , 2 , 3 ) _
     or ry( 4 , 5 , 6 ) _
     or ry( 7 , 8 , 9 ) _
     or ry( 1 , 4 , 7 ) _
     or ry( 2 , 5 , 8 ) _
     or ry( 3 , 6 , 9 )
end function
sub rando.move
  dice = int( rnd(0) * 9 )
  while bord( dice )
    dice = int( rnd(0) * 9 )
  wend
  if game <> rr then
    notice "Rando moves on : " ; dice
  end if
  call any.move dice
end sub
sub ai.move
  q$ = memory$( bordstate )
  p = int( rnd(0) * ( len( q$ ) - 1 ) )
  z = val( mid$( q$ , p+1 , 1 ) )
  notice "AI moves on : " ; z
  call any.move z
end sub
sub leren
  select case tel
    case 3
      call kill.move borden( 2 ) , z( 2 )
    case 4
      call add.move borden( 2 ) , z( 2 )
      call kill.move borden( 3 ) , z( 3 )
    case 5
      call forget.move borden( 2 ) , z( 2 )
      call add.move borden( 3 ) , z( 3 )
      call kill.move borden( 4 ) , z( 4 )
    case else
  end select
end sub
sub forget.move b , z
  p = instr( memory$( b ) , str$( z ) )
  if p = 0 then exit sub
  l$ = left$( memory$( b ) , p )
  r$ = right$( memory$( b ) , len( memory$( b ) ) - p )
  memory$( b ) = l$ + r$
end sub
sub add.move b , z
  if len( memory$( b ) ) < 200  then
    memory$( b ) = memory$( b ) + str$( z )
  end if
end sub
sub kill.move b , z
  while instr( memory$( b ) , str$( z ) )
    call forget.move b , z
  wend
end sub
 
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5471
xx Re: AI game : not 3
« Reply #2 on: Aug 1st, 2017, 06:35am »

When characterInput only works in graphic windows. You could try a hidden textbox to get keypresses but I don't think the game needs keypresses.


Code:
sub save.memory
    open "not3.txt" for output as #uit
    #uit str$(times.played)
    for i = 0 to 511
        #uit memory$( i )
    next i
    close #uit
end sub
sub load.memory
    open "not3.txt" for input as #in
    line input #in , t$
    times.played=val(t$)
    for i = 0 to 511
        line input #in , memory$(i)
    next i
    close #in
end sub
 
User IP Logged

bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 902
xx Re: AI game : not 3
« Reply #3 on: Aug 2nd, 2017, 04:22am »

@ rod :
thanks for file code

it shoot be working now but
error :
menu not working
trapclose not working

and what about the questions in REM ?

Code:
WindowWidth = 600
WindowHeight = 650
global who.play , who1 , who2 , human , rando , ai , tel
global game,hr,rh,rr,hai,aih,bordstate,times.played
human = 1
rando = 2
ai = 3
hr = 1
rh = 2
rr = 3
hai = 4
aih = 5
dim bord( 9 ) , borden( 9 ) , z( 9 ) , memory$( 512 )
button #m.1 , "1" , btn.click , UL ,   0 , 400 , 200 , 200
button #m.2 , "2" , btn.click , UL , 200 , 400 , 200 , 200
button #m.3 , "3" , btn.click , UL , 400 , 400 , 200 , 200
button #m.4 , "4" , btn.click , UL ,   0 , 200 , 200 , 200
button #m.5 , "5" , btn.click , UL , 200 , 200 , 200 , 200
button #m.6 , "6" , btn.click , UL , 400 , 200 , 200 , 200
button #m.7 , "7" , btn.click , UL ,   0 ,   0 , 200 , 200
button #m.8 , "8" , btn.click , UL , 200 ,   0 , 200 , 200
button #m.9 , "9" , btn.click , UL , 400 ,   0 , 200 , 200
menu #m , "Game" _
        , "You vs rando" , [hr] _
        , "Rando vs you" , [rh] _
        , "Learning" , [rr] _
        , "Human vs AI" , [hai] _
        , "AI vs Human" , [aih] _
        , | , "Quit" , [quit]
menu #m , "?" _
        , "about not 3" , [help]
open "NOT 3 : version 2.0" for window as #m
  #m "trapclose [quit]"
  #m "font 100 bold"
''can this be checked automaticly ?
  confirm "first time playing ?" ; ny$
  if ny$ = "yes" then
    call new.memory
  else
    call load.memory
  end if
wait
[quit]
  call save.memory
  close #m
end
[hr]
  game = hr
  who1 = human
  who2 = rando
  call new.game
wait
[rh]
  game = rh
  who1 = rando
  who2 = human
  call new.game
wait
[rr]
  game = rr
  who1 = rando
  who2 = rando
  prompt "how many times ?" ; q$
  t = times.played
  for i = t to t + val( q$ )
    call new.game
    while not( row() )
      call rando.move
    wend
  next i
wait
[hai]
  game = hai
  who1 = human
  who2 = ai
  call new.game
wait
[aih]
  game = aih
  who1 = ai
  who2 = human
  call new.game
wait
[help]
  notice "This game was created by bluatigro ." + chr$( 13 ) _
       + "Object of the game :" + chr$( 13 ) _
       + "Move your X so that the computer" + chr$( 13 ) _
       + "moves the 3e X in a row or colom ." + chr$( 13 ) _
       + chr$( 13 ) ; times.played ; " Times played ."
wait
sub new.game
  for i = 1 to 9
    bord( i ) = 0
  next i
  bord( 0 ) = 1
  #m.1 , "1"
  #m.2 , "2"
  #m.3 , "3"
  #m.4 , "4"
  #m.5 , "5"
  #m.6 , "6"
  #m.7 , "7"
  #m.8 , "8"
  #m.9 , "9"
  tel = 0
  who.play = who1
  bordstate = 0
  if who.play = rando then
    call rando.move
  end if
  if who.play = ai then
    call ai.move
  end if
end sub
sub new.memory
  times.played = 0
  for mem = 0 to 511
    q$ = ""
    for i = 0 to 8
      if not( mem and 2 ^ i ) then
        for t = 0 to 4
          q$ = q$ + str$( i + 1 )
        next t
      end if
    next i
    memory$( mem ) = q$
  next mem
  call save.memory
end sub
sub save.memory
  open "not3.txt" for output as #uit
    #uit str$( times.played )
    for i = 0 to 511
      #uit memory$( i )
    next i
  close #uit
end sub
sub load.memory
  open "not3.txt" for input as #in
    line input #in , t$
    times.played = val( t$ )
    for i = 0 to 511
      line input #in , memory$( i )
    next i
  close #in
end sub
sub btn.click h$
  if who.play = human then
    z = val( right$( h$ , 1 ) )
    if bord( z ) then
      notice "ON A EMPTY PLASE PLEASE !!"
      exit sub
    end if
    call any.move z
  end if
  if game = hr or game = rh then
    call rando.move
  end if
  if game = hai or game = aih then
    call ai.move
  end if
end sub
sub any.move z
  bord( z ) = 1
  z( tel ) = z
  bordstate = bordstate or 2 ^ z
  borden( tel ) = bordstate
  tel = tel + 1
  select case z
    case 1
      #m.1 , "X"
    case 2
      #m.2 , "X"
    case 3
      #m.3 , "X"
    case 4
      #m.4 , "X"
    case 5
      #m.5 , "X"
    case 6
      #m.6 , "X"
    case 7
      #m.7 , "X"
    case 8
      #m.8 , "X"
    case 9
      #m.9 , "X"
    case else
  end select
  if who.play = who1 then
    who.play = who2
  else
    who.play = who1
  end if
  if row() and game <> rr then
    select case who.play
      case human
        notice "You won !!"
      case rando
        notice "rando has won ."
      case else '' ai
        notice "AI has won ."
    end select
  end if
  times.played = times.played + 1
  call leren
end sub
function ry( a , b , c )
  ry = bord(a) and bord(b) and bord(c)
end function
function row()
  row = ry( 1 , 2 , 3 ) _
     or ry( 4 , 5 , 6 ) _
     or ry( 7 , 8 , 9 ) _
     or ry( 1 , 4 , 7 ) _
     or ry( 2 , 5 , 8 ) _
     or ry( 3 , 6 , 9 )
end function
sub rando.move
  dice = int( rnd(0) * 9 )
  while bord( dice )
    dice = int( rnd(0) * 9 )
  wend
  if game <> rr then
    notice "Rando moves on : " ; dice
  end if
  call any.move dice
end sub
sub ai.move
  q$ = memory$( bordstate )
  p = int( rnd(0) * ( len( q$ ) - 1 ) )
  z = val( mid$( q$ , p+1 , 1 ) )
  notice "AI moves on : " ; z
  call any.move z
end sub
sub leren
''is here room for inprovement ?
  select case tel
    case 3
      call add.move 1
      call kil.move 2
    case 4
      call forget.move 1
      call add.move 2
      call kil.move 3
    case 5
      call add.move 1
      call forget.move 2
      call add.move 3
      call kil.move 4
    case else
  end select
end sub
sub forget.move no
  b = borden( no )
  z = z( no )
  p = instr( memory$( b ) , str$( z ) )
  if p = 0 then exit sub
  l$ = left$( memory$( b ) , p )
  r$ = right$( memory$( b ) , len( memory$( b ) ) - p - 1 )
  memory$( b ) = l$ + r$
end sub
sub add.move no
  b = borden( no )
  z = z( no )
  if len( memory$( b ) ) < 200  then
    memory$( b ) = memory$( b ) + str$( z )
  end if
end sub
sub kil.move no
  b = borden( no )
  z = z( no )
  while instr( memory$( b ) , str$( z ) )
    call forget.move no
  wend
end sub
 
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5471
xx Re: AI game : not 3
« Reply #4 on: Aug 2nd, 2017, 3:27pm »

You can't mix branch labels and subs easily. So change all your menu items and quit to subs.

You will need to use the files command to check if the file exists and so load memory or start fresh.
User IP Logged

bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 902
xx Re: AI game : not 3
« Reply #5 on: Aug 3rd, 2017, 03:26am »

@ rod :
thanks for help

update :
the code makes the memory file automaticly
its beginning to look like what i want

error :
the code stops at the 3e move . why ?

i m not sure that the memory gets smarter

Code:
WindowWidth = 600
WindowHeight = 650
global who.play , who1 , who2 , human , rando , ai , tel
global game,hr,rh,rr,hai,aih,bordstate,times.played
human = 1
rando = 2
ai = 3
hr = 1
rh = 2
rr = 3
hai = 4
aih = 5
dim bord( 9 ) , borden( 9 ) , z( 9 ) , memory$( 512 )
dim info$(10,10)
button #m.1 , "1" , btn.click , UL ,   0 , 400 , 200 , 200
button #m.2 , "2" , btn.click , UL , 200 , 400 , 200 , 200
button #m.3 , "3" , btn.click , UL , 400 , 400 , 200 , 200
button #m.4 , "4" , btn.click , UL ,   0 , 200 , 200 , 200
button #m.5 , "5" , btn.click , UL , 200 , 200 , 200 , 200
button #m.6 , "6" , btn.click , UL , 400 , 200 , 200 , 200
button #m.7 , "7" , btn.click , UL ,   0 ,   0 , 200 , 200
button #m.8 , "8" , btn.click , UL , 200 ,   0 , 200 , 200
button #m.9 , "9" , btn.click , UL , 400 ,   0 , 200 , 200
menu #m , "Game" _
        , "You vs rando" , humanrando _
        , "Rando vs you" , randohuman _
        , "Learning" , randorando _
        , "Human vs AI" , humanai _
        , "AI vs Human" , aihuman _
        , | , "Quit" , quit
menu #m , "?" _
        , "about not 3" , help
nomainwin
open "NOT 3 : version 2.0" for window as #m
  #m "trapclose [quit]"
  #m "font 100 bold"
  files DefaultDir$ + "not3.txt" , info$()
  if val( info$(0,0) ) = 0 then
    call new.memory
  else
    call load.memory
  end if
wait
sub quit
  call save.memory
  close #m
  end
end sub
[quit]
  call quit
end
sub humanrando
  game = hr
  who1 = human
  who2 = rando
  call new.game
end sub
sub randohuman
  game = rh
  who1 = rando
  who2 = human
  call new.game
end sub
sub randorando
  game = rr
  who1 = rando
  who2 = rando
  prompt "how many times ?" ; q$
  t = times.played
  for i = t to t + val( q$ )
    call new.game
    while not( row() )
      call rando.move
    wend
  next i
end sub
sub hunanai
  game = hai
  who1 = human
  who2 = ai
  call new.game
end sub
sub aihuman
  game = aih
  who1 = ai
  who2 = human
  call new.game
end sub
sub help
  notice "This game was created by bluatigro ." + chr$( 13 ) _
       + "Object of the game :" + chr$( 13 ) _
       + "Move your X so that the computer" + chr$( 13 ) _
       + "moves the 3e X in a row or colom ." + chr$( 13 ) _
       + chr$( 13 ) ; times.played ; " Times played ."
end sub
sub new.game
  for i = 1 to 9
    bord( i ) = 0
  next i
  bord( 0 ) = 1
  #m.1 , "1"
  #m.2 , "2"
  #m.3 , "3"
  #m.4 , "4"
  #m.5 , "5"
  #m.6 , "6"
  #m.7 , "7"
  #m.8 , "8"
  #m.9 , "9"
  tel = 0
  who.play = who1
  bordstate = 0
  if who.play = rando then
    call rando.move
  end if
  if who.play = ai then
    call ai.move
  end if
end sub
sub new.memory
  times.played = 0
  for mem = 0 to 511
    q$ = ""
    for i = 0 to 8
      if not( mem and 2 ^ i ) then
        for t = 0 to 4
          q$ = q$ + str$( i + 1 )
        next t
      end if
    next i
    memory$( mem ) = q$
  next mem
  call save.memory
end sub
sub save.memory
  open "not3.txt" for output as #uit
    #uit str$( times.played )
    for i = 0 to 511
      #uit memory$( i )
    next i
  close #uit
end sub
sub load.memory
  open "not3.txt" for input as #in
    line input #in , t$
    times.played = val( t$ )
    for i = 0 to 511
      line input #in , memory$( i )
    next i
  close #in
end sub
sub btn.click h$
  if who.play = human then
    z = val( right$( h$ , 1 ) )
    if bord( z ) then
      notice "ON A EMPTY PLASE PLEASE !!"
      exit sub
    end if
    call any.move z
  end if
  if game = hr or game = rh then
    call rando.move
  end if
  if game = hai or game = aih then
    call ai.move
  end if
end sub
sub any.move z
  bord( z ) = 1
  z( tel ) = z
  bordstate = bordstate or 2 ^ z
  borden( tel ) = bordstate
  tel = tel + 1
  select case z
    case 1
      #m.1 , "X"
    case 2
      #m.2 , "X"
    case 3
      #m.3 , "X"
    case 4
      #m.4 , "X"
    case 5
      #m.5 , "X"
    case 6
      #m.6 , "X"
    case 7
      #m.7 , "X"
    case 8
      #m.8 , "X"
    case 9
      #m.9 , "X"
    case else
  end select
  if who.play = who1 then
    who.play = who2
  else
    who.play = who1
  end if
  if row() and game <> rr then
    select case who.play
      case human
        notice "You won !!"
      case rando
        notice "rando has won ."
      case else '' ai
        notice "AI has won ."
    end select
  end if
  times.played = times.played + 1
  call leren
end sub
function ry( a , b , c )
  ry = bord(a) and bord(b) and bord(c)
end function
function row()
  row = ry( 1 , 2 , 3 ) _
     or ry( 4 , 5 , 6 ) _
     or ry( 7 , 8 , 9 ) _
     or ry( 1 , 4 , 7 ) _
     or ry( 2 , 5 , 8 ) _
     or ry( 3 , 6 , 9 )
end function
sub rando.move
  dice = int( rnd(0) * 9 )
  while bord( dice )
    dice = int( rnd(0) * 9 )
  wend
  if game <> rr then
    notice "Rando moves on : " ; dice
  end if
  call any.move dice
end sub
sub ai.move
  q$ = memory$( bordstate )
  p = int( rnd(0) * ( len( q$ ) - 1 ) )
  z = val( mid$( q$ , p+1 , 1 ) )
  notice "AI moves on : " ; z
  call any.move z
end sub
sub leren
''is here room for inprovement ?
  select case tel
    case 3
      call add.move 1
      call kil.move 2
    case 4
      call forget.move 1
      call add.move 2
      call kil.move 3
    case 5
      call add.move 1
      call forget.move 2
      call add.move 3
      call kil.move 4
    case else
  end select
end sub
sub forget.move no
  b = borden( no )
  z = z( no )
  p = instr( memory$( b ) , str$( z ) )
  if p = 0 then exit sub
  l$ = left$( memory$( b ) , p )
  r$ = right$( memory$( b ) , len( memory$( b ) ) - p - 1 )
  memory$( b ) = l$ + r$
end sub
sub add.move no
  b = borden( no )
  z = z( no )
  if len( memory$( b ) ) < 200  then
    memory$( b ) = memory$( b ) + str$( z )
  end if
end sub
sub kil.move no
  b = borden( no )
  z = z( no )
  while instr( memory$( b ) , str$( z ) )
    call forget.move no
  wend
end sub
 
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 902
xx Re: AI game : not 3
« Reply #6 on: Aug 3rd, 2017, 03:36am »

update :
3e move freze fixed
error :
the code frezes when game over
Code:
sub any.move z
  bord( z ) = 1
  z( tel ) = z
  bordstate = bordstate or 2 ^ z
  borden( tel ) = bordstate
  tel = tel + 1
  select case z
    case 1
      #m.1 , "X"
    case 2
      #m.2 , "X"
    case 3
      #m.3 , "X"
    case 4
      #m.4 , "X"
    case 5
      #m.5 , "X"
    case 6
      #m.6 , "X"
    case 7
      #m.7 , "X"
    case 8
      #m.8 , "X"
    case 9
      #m.9 , "X"
    case else
  end select
  if who.play = who1 then
    who.play = who2
  else
    who.play = who1
  end if
  if row() then
    if game <> rr then
      select case who.play
        case human
          notice "You won !!"
        case rando
          notice "rando has won ."
        case else '' ai
          notice "AI has won ."
      end select
    end if
    times.played = times.played + 1
    call leren
  end if
end sub
 
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 902
xx Re: AI game : not 3
« Reply #7 on: Aug 3rd, 2017, 04:16am »

version whitout learning
so you can play the game
Code:
WindowWidth = 600
WindowHeight = 650
global who.play , who1 , who2 , human , rando , r1000
global game , hr , rh , hr1 , r1h
human = 1
rando = 2
r1000 = 3
hr = 1
rh = 2
hr1 = 3
r1h = 4
dim bord( 9 )
nomainwin
button #m.1 , "1" , btn.click , UL , 0 , 400 , 200 , 200
button #m.2 , "2" , btn.click , UL , 200 , 400 , 200 , 200
button #m.3 , "3" , btn.click , UL , 400 , 400 , 200 , 200
button #m.4 , "4" , btn.click , UL , 0 , 200 , 200 , 200
button #m.5 , "5" , btn.click , UL , 200 , 200 , 200 , 200
button #m.6 , "6" , btn.click , UL , 400 , 200 , 200 , 200
button #m.7 , "7" , btn.click , UL , 0 , 0 , 200 , 200
button #m.8 , "8" , btn.click , UL , 200 , 0 , 200 , 200
button #m.9 , "9" , btn.click , UL , 400 , 0 , 200 , 200
menu #m , "Game" _
        , "You vs rando" , humanrando _
        , "Rando vs you" , randohuman _
        , "You vs r1000" , humanr1000 _
        , "r1000 vs you" , r1000human _
        , | , "Quit" , quit

open "NOT 3" for window as #m
  #m "trapclose [quit]"
  #m "font 50 bold"
wait
sub quit
  close #m
end sub
[quit]
  close #m
end
sub humanrando
  who1 = human
  who2 = rando
  game = hr
  call new.game
end sub
sub randohuman
  who1 = rando
  who2 = human
  game = rh
  call new.game
end sub
sub humanr1000
  who1 = human
  who2 = r1000
  game = hr1
  call new.game
end sub
sub r1000human
  who1 = r1000
  who2 = human
  game = r1h
  call new.game
end sub
sub new.game
  for i = 1 to 9
    bord( i ) = 0
  next i
  bord( 0 ) = 1
  #m.1 , "1"
  #m.2 , "2"
  #m.3 , "3"
  #m.4 , "4"
  #m.5 , "5"
  #m.6 , "6"
  #m.7 , "7"
  #m.8 , "8"
  #m.9 , "9"
  who.play = who1
  bordstate = 0
  if who.play = rando then
    call rando.zet
  end if
  if who.play = r1000 then
    call r1000.zet
  end if
end sub
sub btn.click h$
  if who.play = human then
    z = val( right$( h$ , 1 ) )
    if bord( z ) then
      notice "ON A EMPTY PLASE PLEASE !!"
      exit sub
    end if
    call any.zet z
  end if
  if game = hr or game = rh then
    call rando.zet
  end if
  if game = hr1 or game = r1h then
    call r1000.zet
  end if
end sub
sub any.zet z
  bord( z ) = 1
  select case z
    case 1
      #m.1 , "X"
    case 2
      #m.2 , "X"
    case 3
      #m.3 , "X"
    case 4
      #m.4 , "X"
    case 5
      #m.5 , "X"
    case 6
      #m.6 , "X"
    case 7
      #m.7 , "X"
    case 8
      #m.8 , "X"
    case 9
      #m.9 , "X"
    case else
  end select
  if who.play = who1 then
    who.play = who2
  else
    who.play = who1
  end if
  if row() then
    select case who.play
      case human
        notice "You won !!"
      case else ''rando
        notice "You lost ."
    end select
  end if
end sub
function ry( a , b , c )
  ry = bord(a) and bord(b) and bord(c)
end function
function row()
  row = ry( 1 , 2 , 3 ) _
     or ry( 4 , 5 , 6 ) _
     or ry( 7 , 8 , 9 ) _
     or ry( 1 , 4 , 7 ) _
     or ry( 2 , 5 , 8 ) _
     or ry( 3 , 6 , 9 )
end function
sub rando.zet
  dice = int( rnd(0) * 9 )
  while bord( dice )
    dice = int( rnd(0) * 9 )
  wend
  notice "Rando moves on : " ; dice
  call any.zet dice
end sub
sub r1000.zet
  i = 0
  done = 0
  do
    i = i + 1
    dice = int( rnd(0) * 9 )
    while bord( dice )
      dice = int( rnd(0) * 9 )
    wend
    bord( dice ) = 1
    if not( row() ) then done = 1
    bord( dice ) = 0
  loop while not( done ) and i < 1000
  notice "R1000 moves on " ; dice
  call any.zet dice
end sub




 
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls