Code: Select all
; Game Console "Sea battle"
; The installation of ships is automatic.
; Two difficulty levels
; kvitaliy, 2017
; All Free
;
Global Dim My_ships.s(11,11)
Global Dim Enemy_ships.s(11,11)
Macro MI ; mission Impossible - beep api
Beep_(784,150);
Sleep_(300);
Beep_(784,150);
Sleep_(300);
Beep_(932,150);
Sleep_(150);
Beep_(1047,150);
Sleep_(150);
Beep_(784,150);
Sleep_(300);
Beep_(784,150);
Sleep_(300);
Beep_(699,150);
Sleep_(150);
Beep_(740,150);
Sleep_(150);
Beep_(784,150);
Sleep_(300);
Beep_(784,150);
Sleep_(300);
Beep_(932,150);
Sleep_(150);
Beep_(1047,150);
Sleep_(150);
Beep_(784,150);
Sleep_(300);
Beep_(784,150);
Sleep_(300);
Beep_(699,150);
Sleep_(150);
Beep_(740,150);
Sleep_(150);
Beep_(932,150);
Beep_(784,150);
Beep_(587,1200);
Sleep_(75);
Beep_(932,150);
Beep_(784,150);
Beep_(554,1200);
Sleep_(75);
Beep_(932,150);
Beep_(784,150);
Beep_(523,1200);
Sleep_(150);
Beep_(466,150);
Beep_(523, 150);
EndMacro
Macro ODD (n)
(n & 1)
EndMacro
Procedure ClearStr(n,x=5,y=20)
ConsoleLocate(x,y)
For i=1 To n
PrintN(" ")
Next
ConsoleLocate(x,y)
EndProcedure
Procedure PrintMy_ships()
ConsoleLocate(15,1)
Print( "S_E_A B_A_T_T_L_E ")
ConsoleLocate(5,4)
Print("A B C D E F G H I J")
sy=5
For I = 1 To 10
ConsoleLocate(3,sy)
sy+1
Print(""+i)
Next
sx=5
sy=5
For x = 1 To 10
For y = 1 To 10
ConsoleLocate(sx,sy)
Print (""+My_ships(x,y))
If sx<23:sx+2:Else:sy+1:sx=5:EndIf
Next
Next
EndProcedure
Procedure PrintEnemy_ships(Visible.b )
ConsoleLocate(30,4)
Print("A B C D E F G H I J")
sy=5
For I = 1 To 10
ConsoleLocate(28,sy)
sy+1
Print(""+i)
Next
sx=30
sy=5
For x = 1 To 10
For y = 1 To 10
ConsoleLocate(sx,sy)
If Visible
Print (""+Enemy_ships(x,y))
Else
If Enemy_ships(x,y)="◙"
Print ("~");"·"
Else
Print (""+Enemy_ships(x,y))
EndIf
EndIf
If sx<48:sx+2:Else:sy+1:sx=30:EndIf
Next
Next
EndProcedure
Procedure Ships4x(enemy=1)
;- 4x = 1
x=Random(10,1)
y=Random(10,1)
If ODD(x)
If x<=7
For i=x To x+3
If enemy
Enemy_ships(i,y)="◙"
Else
My_ships(i,y)="◙"
EndIf
Next
ElseIf x>=8
For i=x-3 To x
If enemy
Enemy_ships(i,y)="◙"
Else
My_ships(i,y)="◙"
EndIf
Next
EndIf
Else
If y<=7
For i=y To y+3
If enemy
Enemy_ships(i,y)="◙"
Else
My_ships(i,y)="◙"
EndIf
Next
ElseIf y>=8
For i=y-3 To y
If enemy
Enemy_ships(i,y)="◙"
Else
My_ships(i,y)="◙"
EndIf
Next
EndIf
EndIf
EndProcedure
Procedure Ships3x(enemy=1)
;- 3x = 2
n=0
Repeat
x=Random(10,1)
y=Random(10,1)
If enemy
;rule check
If Enemy_ships(x,y) <>"◙" And Enemy_ships(x+1,y) <>"◙" And Enemy_ships(x-1,y) <>"◙" And Enemy_ships(x,y-1) <>"◙" And Enemy_ships(x,y+1) <>"◙" And Enemy_ships(x-1,y+1) <>"◙" And Enemy_ships(x+1,y+1) <>"◙" And Enemy_ships(x+1,y-1) <>"◙" And Enemy_ships(x-1,y-1) <>"◙"
n=1
Else
n=0
EndIf
Else
If My_ships(x,y) <>"◙" And My_ships(x+1,y) <>"◙" And My_ships(x-1,y) <>"◙" And My_ships(x,y-1) <>"◙" And My_ships(x,y+1) <>"◙" And My_ships(x-1,y+1) <>"◙" And My_ships(x+1,y+1) <>"◙" And My_ships(x+1,y-1) <>"◙" And My_ships(x-1,y-1) <>"◙"
n=1
Else
n=0
EndIf
EndIf
Until n=1
If enemy
If ODD(x) ; horizontally or vertically
If x<=8
For i=x To x+2
;rule check
If Enemy_ships(i,y) <>"◙" And Enemy_ships(i,y+1) <>"◙" And Enemy_ships(i,y-1) <>"◙" And Enemy_ships(i+1,y) <>"◙" And Enemy_ships(i+1,y+1) <>"◙" And Enemy_ships(i+1,y-1) <>"◙"
Enemy_ships(i,y)="◙"
Else
ProcedureReturn 0
EndIf
Next
ElseIf x>=9
For i=x-2 To x
; rule check
If Enemy_ships(i,y) <>"◙" And Enemy_ships(i,y+1) <>"◙" And Enemy_ships(i,y-1) <>"◙" And Enemy_ships(i-1,y) <>"◙" And Enemy_ships(i-1,y-1) <>"◙" And Enemy_ships(i-1,y+1) <>"◙"
Enemy_ships(i,y)="◙"
Else
ProcedureReturn 0
EndIf
Next
EndIf
Else
If y<=8
For i=y To y+2
; rule check
If Enemy_ships(x,i) <>"◙" And Enemy_ships(x+1,i) <>"◙" And Enemy_ships(x-1,i) <>"◙" And Enemy_ships(x,i+1) <>"◙" And Enemy_ships(x-1,i+1) <>"◙" And Enemy_ships(x+1,i+1) <>"◙"
Enemy_ships(x,i)="◙"
Else
ProcedureReturn 0
EndIf
Next
ElseIf y>=9
For i=y-2 To y
; rule check
If Enemy_ships(x,i) <>"◙" And Enemy_ships(x+1,i) <>"◙" And Enemy_ships(x-1,i) <>"◙" And Enemy_ships(x,i-1) <>"◙" And Enemy_ships(x+1,i-1) <>"◙" And Enemy_ships(x-1,i-1) <>"◙"
Enemy_ships(x,i)="◙"
Else
ProcedureReturn 0
EndIf
Next
EndIf
EndIf
Else
If ODD(x) ; horizontally or vertically
If x<=8
For i=x To x+2
; rule check
If My_ships(i,y) <>"◙" And My_ships(i,y+1) <>"◙" And My_ships(i,y-1) <>"◙" And My_ships(i+1,y) <>"◙" And My_ships(i+1,y+1) <>"◙" And My_ships(i+1,y-1) <>"◙"
My_ships(i,y)="◙"
Else
ProcedureReturn 0
EndIf
Next
ElseIf x>=9
For i=x-2 To x
; rule check
If My_ships(i,y) <>"◙" And My_ships(i,y+1) <>"◙" And My_ships(i,y-1) <>"◙" And My_ships(i-1,y) <>"◙" And My_ships(i-1,y-1) <>"◙" And My_ships(i-1,y+1) <>"◙"
My_ships(i,y)="◙"
Else
ProcedureReturn 0
EndIf
Next
EndIf
Else
If y<=8
For i=y To y+2
; rule check
If My_ships(x,i) <>"◙" And My_ships(x+1,i) <>"◙" And My_ships(x-1,i) <>"◙" And My_ships(x,i+1) <>"◙" And My_ships(x-1,i+1) <>"◙" And My_ships(x+1,i+1) <>"◙"
My_ships(x,i)="◙"
Else
ProcedureReturn 0
EndIf
Next
ElseIf y>=9
For i=y-2 To y
; rule check
If My_ships(x,i) <>"◙" And My_ships(x+1,i) <>"◙" And My_ships(x-1,i) <>"◙" And My_ships(x,i-1) <>"◙" And My_ships(x+1,i-1) <>"◙" And My_ships(x-1,i-1) <>"◙"
My_ships(x,i)="◙"
Else
ProcedureReturn 0
EndIf
Next
EndIf
EndIf
EndIf
ProcedureReturn 1
EndProcedure
Procedure Ships2x(enemy=1)
;- 2x = 3
n=0
Repeat
x=Random(10,1)
y=Random(10,1)
If enemy
;rule check
If Enemy_ships(x,y) <>"◙" And Enemy_ships(x+1,y) <>"◙" And Enemy_ships(x-1,y) <>"◙" And Enemy_ships(x,y-1) <>"◙" And Enemy_ships(x,y+1) <>"◙" And Enemy_ships(x-1,y+1) <>"◙" And Enemy_ships(x+1,y+1) <>"◙" And Enemy_ships(x+1,y-1) <>"◙" And Enemy_ships(x-1,y-1) <>"◙"
n=1
Else
n=0
EndIf
Else
If My_ships(x,y) <>"◙" And My_ships(x+1,y) <>"◙" And My_ships(x-1,y) <>"◙" And My_ships(x,y-1) <>"◙" And My_ships(x,y+1) <>"◙" And My_ships(x-1,y+1) <>"◙" And My_ships(x+1,y+1) <>"◙" And My_ships(x+1,y-1) <>"◙" And My_ships(x-1,y-1) <>"◙"
n=1
Else
n=0
EndIf
EndIf
Until n=1
If enemy
If ODD(x) ; horizontally or vertically
If x<=9
For i=x To x+1
;rule check
If Enemy_ships(i,y) <>"◙" And Enemy_ships(i,y+1) <>"◙" And Enemy_ships(i,y-1) <>"◙" And Enemy_ships(i+1,y) <>"◙" And Enemy_ships(i+1,y+1) <>"◙" And Enemy_ships(i+1,y-1) <>"◙"
Enemy_ships(i,y)="◙"
Else
ProcedureReturn 0
EndIf
Next
ElseIf x>=10
For i=x-1 To x
;rule check
If Enemy_ships(i,y) <>"◙" And Enemy_ships(i,y+1) <>"◙" And Enemy_ships(i,y-1) <>"◙" And Enemy_ships(i-1,y) <>"◙" And Enemy_ships(i-1,y-1) <>"◙" And Enemy_ships(i-1,y+1) <>"◙"
Enemy_ships(i,y)="◙"
Else
ProcedureReturn 0
EndIf
Next
EndIf
Else
If y<=9
For i=y To y+1
;rule check
If Enemy_ships(x,i) <>"◙" And Enemy_ships(x+1,i) <>"◙" And Enemy_ships(x-1,i) <>"◙" And Enemy_ships(x,i+1) <>"◙" And Enemy_ships(x-1,i+1) <>"◙" And Enemy_ships(x+1,i+1) <>"◙"
Enemy_ships(x,i)="◙"
Else
ProcedureReturn 0
EndIf
Next
ElseIf y>=10
For i=y-1 To y
;rule check
If Enemy_ships(x,i) <>"◙" And Enemy_ships(x+1,i) <>"◙" And Enemy_ships(x-1,i) <>"◙" And Enemy_ships(x,i-1) <>"◙" And Enemy_ships(x+1,i-1) <>"◙" And Enemy_ships(x-1,i-1) <>"◙"
Enemy_ships(x,i)="◙"
Else
ProcedureReturn 0
EndIf
Next
EndIf
EndIf
Else
If ODD(x) ; horizontally or vertically
If x<=9
For i=x To x+1
;rule check
If My_ships(i,y) <>"◙" And My_ships(i,y+1) <>"◙" And My_ships(i,y-1) <>"◙" And My_ships(i+1,y) <>"◙" And My_ships(i+1,y+1) <>"◙" And My_ships(i+1,y-1) <>"◙"
My_ships(i,y)="◙"
Else
ProcedureReturn 0
EndIf
Next
ElseIf x>=10
For i=x-1 To x
;rule check
If My_ships(i,y) <>"◙" And My_ships(i,y+1) <>"◙" And My_ships(i,y-1) <>"◙" And My_ships(i-1,y) <>"◙" And My_ships(i-1,y-1) <>"◙" And My_ships(i-1,y+1) <>"◙"
My_ships(i,y)="◙"
Else
ProcedureReturn 0
EndIf
Next
EndIf
Else
If y<=9
For i=y To y+1
;rule check
If My_ships(x,i) <>"◙" And My_ships(x+1,i) <>"◙" And My_ships(x-1,i) <>"◙" And My_ships(x,i+1) <>"◙" And My_ships(x-1,i+1) <>"◙" And My_ships(x+1,i+1) <>"◙"
My_ships(x,i)="◙"
Else
ProcedureReturn 0
EndIf
;My_ships(x,i)="◙"
Next
ElseIf y>=10
For i=y-1 To y
;rule check
If My_ships(x,i) <>"◙" And My_ships(x+1,i) <>"◙" And My_ships(x-1,i) <>"◙" And My_ships(x,i-1) <>"◙" And My_ships(x+1,i-1) <>"◙" And My_ships(x-1,i-1) <>"◙"
My_ships(x,i)="◙"
Else
ProcedureReturn 0
EndIf
;My_ships(x,i)="◙"
Next
EndIf
EndIf
EndIf
ProcedureReturn 1
EndProcedure
Procedure Ships1x(enemy=1)
;- 1x = 4
n=0
If enemy
Repeat
x=Random(10,1)
y=Random(10,1)
;rule check
If Enemy_ships(x,y) <>"◙" And Enemy_ships(x+1,y) <>"◙" And Enemy_ships(x-1,y) <>"◙" And Enemy_ships(x,y-1) <>"◙" And Enemy_ships(x,y+1) <>"◙" And Enemy_ships(x-1,y+1) <>"◙" And Enemy_ships(x+1,y+1) <>"◙" And Enemy_ships(x+1,y-1) <>"◙" And Enemy_ships(x-1,y-1) <>"◙"
Enemy_ships(x,y)="◙"
n=1
Else
n=0
EndIf
Until n=1
Else
Repeat
x=Random(10,1)
y=Random(10,1)
;rule check
If My_ships(x,y) <>"◙" And My_ships(x+1,y) <>"◙" And My_ships(x-1,y) <>"◙" And My_ships(x,y-1) <>"◙" And My_ships(x,y+1) <>"◙" And My_ships(x-1,y+1) <>"◙" And My_ships(x+1,y+1) <>"◙" And My_ships(x+1,y-1) <>"◙" And My_ships(x-1,y-1) <>"◙"
My_ships(x,y)="◙"
n=1
Else
n=0
EndIf
Until n=1
EndIf
ProcedureReturn 1
EndProcedure
Procedure RND_Enemy_ships()
Repeat
; clear ships
For x = 1 To 10
For y = 1 To 10
Enemy_ships(x,y) = "~"
Next
Next
n=0:a=0:n2=0:a2=0
; 4x=1
Ships4x()
; 3x=2
For i = 1 To 2
a = Ships3x()
n+a
Next
;2x=3
For i = 1 To 3
a2 = Ships2x()
n2+a2
Next
;1x=4
For i = 1 To 4
Ships1x()
Next
Until n = 2 And n2=3
EndProcedure
Procedure RND_My_ships()
Repeat
; clear ships
For x = 1 To 10
For y = 1 To 10
My_ships(x,y) ="~"
Next
Next
n=0:a=0:n2=0:a2=0
; 4x=1
Ships4x(0)
; 3x=2
For i = 1 To 2
a = Ships3x(0)
n+a
Next
;2x=3
For i = 1 To 3
a2 = Ships2x(0)
n2+a2
Next
;1x=4
For i = 1 To 4
Ships1x(0)
Next
Until n = 2 And n2=3
EndProcedure
Procedure My_Shot()
Static DeadEnemy
Krd.s=""
Repeat
i+1
ClearStr(3)
ConsoleColor(9, 0)
Print ("Your Shot " + i + " => ")
Krd.s=UCase(Input())
If #PB_OS_Windows
Beep_(500,300)
EndIf
If Krd=""
x=Random(10,1)
y=Random(10,1)
Else
x=Val(Right(Krd,1))
If x=0:x=10:EndIf
Select Left(Krd,1)
Case "A", "1"
y=1
Case "B", "2"
y=2
Case "C", "3"
y=3
Case "D", "4"
y=4
Case "E", "5"
y=5
Case "F", "6"
y=6
Case "G", "7"
y=7
Case "H", "8"
y=8
Case "I", "9"
y=9
Case "J", "0"
y=10
EndSelect
EndIf
ConsoleColor(7, 0)
If Enemy_ships(x,y) = "◙"
Enemy_ships(x,y) = "†"
DeadEnemy+1
Else
Enemy_ships(x,y) ="☼"
PrintMy_ships()
PrintEnemy_ships(0) ; 1 visible ships. 0 not
ProcedureReturn DeadEnemy
EndIf
PrintMy_ships()
PrintEnemy_ships(0) ; 1 visible ships. 0 not
Until DeadEnemy=20
ProcedureReturn DeadEnemy
EndProcedure
Procedure Enemy_Shot()
Static MyDead
x=Random(10,1)
y=Random(10,1)
If #PB_OS_Windows
Beep_(800,300)
EndIf
Repeat
If My_ships(x,y) = "†"
x=Random(10,1)
y=Random(10,1)
EndIf
If My_ships(x,y) ="☼"
x=Random(10,1)
y=Random(10,1)
EndIf
If My_ships(x,y) ="~"
My_ships(x,y) ="☼"
PrintMy_ships()
PrintEnemy_ships(0) ; 1 visible ships. 0 not
ProcedureReturn MyDead
EndIf
If My_ships(x,y) = "◙"
My_ships(x,y) = "†"
MyDead+1
PrintMy_ships()
PrintEnemy_ships(0) ; 1 visible ships. 0 not
x=Random(10,1)
y=Random(10,1)
EndIf
Until MyDead=20
ProcedureReturn MyDead
EndProcedure
Procedure Enemy_ShotBrain1()
Static MyDead
x=Random(10,1)
y=Random(10,1)
If #PB_OS_Windows
Beep_(800,300)
EndIf
Repeat
If My_ships(x,y) = "†"
x=Random(10,1)
y=Random(10,1)
EndIf
If My_ships(x,y) = "•"
x=Random(10,1)
y=Random(10,1)
EndIf
If My_ships(x,y) ="☼"
x=Random(10,1)
y=Random(10,1)
EndIf
If My_ships(x,y) ="~"
My_ships(x,y) ="☼"
PrintMy_ships()
PrintEnemy_ships(0) ; 1 visible ships. 0 not
ProcedureReturn MyDead
EndIf
If My_ships(x,y) = "◙"
My_ships(x,y) = "†"
MyDead+1
If My_ships(x+1,y) ="~":My_ships(x+1,y) ="•":EndIf
If My_ships(x,y+1) ="~":My_ships(x,y+1) ="•":EndIf
If My_ships(x+1,y+1) ="~":My_ships(x+1,y+1) ="•":EndIf
If My_ships(x-1,y) = "~":My_ships(x-1,y) ="•":EndIf
If My_ships(x,y-1) ="~":My_ships(x,y-1) ="•":EndIf
If My_ships(x-1,y-1) ="~":My_ships(x-1,y-1) ="•":EndIf
If My_ships(x-1,y+1) ="~":My_ships(x-1,y+1) ="•":EndIf
If My_ships(x+1,y-1) ="~":My_ships(x+1,y-1) ="•":EndIf
PrintMy_ships()
PrintEnemy_ships(0) ; 1 visible ships. 0 not
x=Random(10,1)
y=Random(10,1)
EndIf
Until MyDead=20
ProcedureReturn MyDead
EndProcedure
;- RND All ships
RND_Enemy_ships()
RND_My_ships()
If OpenConsole()
EnableGraphicalConsole(1)
ConsoleLocate(15,1)
Print( "S_E_A B_A_T_T_L_E ")
ConsoleLocate(5,4)
PrintN( "Choose complexity:")
PrintN( "1 - Level Stupid")
PrintN( "2 - Level Brainy")
PrintN( "Select 1 or 2 and press Enter ")
ConsoleLocate(5,20)
PrintN("Mini help how to shoot:")
PrintN( "Player Shot -> A1, B2, F7, and the like. ")
PrintN( "Short change of 10 to 0, letters to numbers, i.e. A10 = A0 and a=A and A1=11 ")
PrintN( "Player random shot -> press Enter")
ConsoleLocate(30,7)
level$ = Input()
If level$ <> "2" : level$ ="1": EndIf
ClearConsole()
PrintMy_ships()
PrintEnemy_ships(0) ; 1 visible ships. 0 not
ConsoleLocate(5,20)
Repeat
My_Win = My_Shot()
If level$ ="1"
Enemy_Win = Enemy_Shot()
Else
Enemy_Win = Enemy_ShotBrain1();
EndIf
ConsoleLocate(55,4)
Select level$
Case "1"
Score$="Level Stupid "
Case "2"
Score$="Level Brainy "
EndSelect
ConsoleColor(9, 0)
Print( Score$)
ConsoleLocate(53,5)
Print( "You Score: " + My_Win)
ConsoleLocate(53,6)
Print( "Computer Score: " + Enemy_Win)
ConsoleColor(7, 0)
Until My_Win =20 Or Enemy_Win=20
PrintMy_ships()
PrintEnemy_ships(1) ; 1 visible ships. 0 not
ClearStr(3)
If My_Win =20
Print("Your victory!")
Else
Print("Computer wins!")
EndIf
If #PB_OS_Windows
MI
EndIf
Input()
EndIf