Merge pull request #589 from masykur/main

Added Tictactoe in Golang
This commit is contained in:
Jeff Atwood
2022-02-26 12:27:44 -06:00
committed by GitHub
4 changed files with 200 additions and 4 deletions

View File

@@ -33,13 +33,15 @@ while(true) {
d = move(c + 3); d = move(c + 3);
computerMoves(d); computerMoves(d);
s = readYourMove(); s = readYourMove();
if (s != move(d + 4)) { if (s == move(d + 4)) {
e = move(d + 4);
computerMoves(e);
}
e = move(d + 6); e = move(d + 6);
computerMoves(e); computerMoves(e);
Console.WriteLine("THE GAME IS A DRAW."); Console.WriteLine("THE GAME IS A DRAW.");
} else {
e = move(d + 4);
computerMoves(e);
Console.WriteLine("AND WINS ********");
}
} else { } else {
d = move(c + 7); d = move(c + 7);
computerMoves(d); computerMoves(d);

View File

@@ -0,0 +1,3 @@
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
Conversion to [Pascal](https://en.wikipedia.org/wiki/Pascal_(programming_language))

View File

@@ -0,0 +1,88 @@
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Printf("Hello, World!\n")
// Print text on the screen with 30 spaces before text
fmt.Printf("%30s\n", "TIC TAC TOE")
// Print text on screen with 15 spaces before text
// And print three lines break on screen
fmt.Printf("%15s\n\n\n\n", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
// THIS PROGRAM PLAYS TIC TAC TOE
// THE MACHINE GOES FIRST
fmt.Printf("THE GAME BOARD IS NUMBERED:\n\n")
fmt.Println("1 2 3")
fmt.Println("8 9 4")
fmt.Println("7 6 5")
// Main program
for {
var (
a, b, c, d, e int
p, q, r, s int
)
a = 9
fmt.Printf("\n\n")
computerMoves(a)
p = readYourMove()
b = move(p + 1)
computerMoves(b)
q = readYourMove()
if q == move(b+4) {
c = move(b + 2)
computerMoves(c)
r = readYourMove()
if r == move(c+4) {
if p%2 != 0 {
d = move(c + 3)
computerMoves(d)
s = readYourMove()
if s == move(d+4) {
e = move(d + 6)
computerMoves(e)
fmt.Println("THE GAME IS A DRAW.")
} else {
e = move(d + 4)
computerMoves(e)
fmt.Println("AND WINS ********")
}
} else {
d = move(c + 7)
computerMoves(d)
fmt.Println("AND WINS ********")
}
} else {
d = move(c + 4)
computerMoves(d)
fmt.Println("AND WINS ********")
}
} else {
c = move(b + 4)
computerMoves(c)
fmt.Println("AND WINS ********")
}
}
}
func computerMoves(move int) {
fmt.Printf("COMPUTER MOVES %v\n", move)
}
func readYourMove() int {
for {
fmt.Printf("YOUR MOVE?")
var input string
fmt.Scan(&input)
number, err := strconv.Atoi(input)
if err == nil {
return number
}
}
}
func move(number int) int {
return number - 8*(int)((number-1)/8)
}

View File

@@ -0,0 +1,103 @@
program tictactoe1;
var
a, b, c, d, e: integer;
p, q, r, s: integer;
procedure computerMoves(m: integer);
begin
write('COMPUTER MOVES ');
writeln(m);
end;
function readYourMove() : integer;
var number: integer;
begin
write('YOUR MOVE?');
readln(number);
readYourMove := number;
end;
function move(number: integer): integer;
begin
move := number - 8 * trunc((number - 1) / 8);
end;
function padLeft(m: string; n: integer): string;
var tmp: string;
begin
tmp := '';
repeat
tmp := tmp + ' ';
n := n - 1;
until n = 0;
tmp := tmp + m;
padLeft := tmp;
end;
begin
writeln(padLeft('TIC TAC TOE', 30));
writeln(padLeft('CREATIVE COMPUTING MORRISTOWN, NEW JERSEY', 15));
writeln('');
writeln('');
writeln('');
writeln('THE GAME BOARD IS NUMBERED:');
writeln('');
writeln('1 2 3');
writeln('8 9 4');
writeln('7 6 5');
while(true) do
begin
writeln('');
writeln('');
a := 9;
computerMoves(a);
p := readYourMove();
b := move(p + 1);
computerMoves(b);
q := readYourMove();
if (q = move(b + 4)) then
begin
c := move(b + 2);
computerMoves(c);
r := readYourMove();
if (r = move(c + 4)) then
begin
if (p mod 2 <> 0) then
begin
d := move(c + 3);
computerMoves(d);
s := readYourMove();
if (s = move(d + 4)) then
begin
e := move(d + 6);
computerMoves(e);
writeln('THE GAME IS A DRAW.');
end
else
begin
e := move(d + 4);
computerMoves(e);
writeln('AND WINS ********');
end
end
else
begin
d := move(c + 7);
computerMoves(d);
writeln('AND WINS ********');
end
end
else
begin
d := move(c + 4);
computerMoves(d);
writeln('AND WINS ********');
end
end
else
begin
c := move(b + 4);
computerMoves(c);
writeln('AND WINS ********');
end;
end;
end.