Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
C
CPTR142_AI_Game
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Christian Rippe
CPTR142_AI_Game
Commits
11d86453
Commit
11d86453
authored
Mar 20, 2019
by
Jason Riggs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change
parent
a666f285
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
54 additions
and
44 deletions
+54
-44
AIplayer.cpp
AIplayer.cpp
+8
-8
AIplayer.h
AIplayer.h
+3
-3
Board.cpp
Board.cpp
+14
-9
Board.h
Board.h
+3
-2
mainOthello.cpp
mainOthello.cpp
+26
-22
No files found.
AIplayer.cpp
View file @
11d86453
...
...
@@ -6,21 +6,21 @@
using
namespace
std
;
pair
<
int
,
int
>
AIplayer
::
move1
()
{
pair
<
int
,
int
>
AIplayer
::
move1
(
displayBoard
AImove
)
{
// declare variables
displayBoard
AImove
;
int
tempRow
,
tempCol
,
x
=
0
,
y
=
0
;
int
x
=
0
,
y
=
0
;
pair
<
int
,
int
>
AIpair
;
// loop until valid move is found
for
(
int
row
=
0
;
row
<
8
;
++
row
)
{
for
(
int
col
=
0
;
col
<
8
;
++
col
)
{
tempRow
=
row
;
tempCol
=
col
;
if
(
AImove
.
isValidAdjacent
(
tempRow
,
tempCol
,
'X'
)
&&
AImove
.
isValidFlip
(
tempRow
,
tempCol
,
'O'
))
{
x
=
tempRow
;
y
=
tempCol
;
bool
flag1
=
AImove
.
isValidAdjacent
(
row
,
col
,
'X'
);
bool
flag2
=
AImove
.
isValidFlip
(
row
,
col
,
'O'
);
cout
<<
row
<<
" "
<<
col
<<
" "
<<
flag1
<<
" "
<<
flag2
<<
endl
;
if
(
AImove
.
isValidAdjacent
(
row
,
col
,
'X'
)
&&
AImove
.
isValidFlip
(
row
,
col
,
'O'
))
{
x
=
row
;
y
=
col
;
}
}
}
...
...
AIplayer.h
View file @
11d86453
#include <cstdlib>
#include <utility>
#include "Board.h"
using
namespace
std
;
class
Error
;
class
displayBoard
;
class
AIplayer
{
public:
AIplayer
()
{
srand
(
0
);
}
// basic constructor to seed random number generator
AIplayer
(
int
seed
)
{
srand
(
seed
);
}
// constructor to seed random number generator with desired seed
pair
<
int
,
int
>
move1
();
// easy move
pair
<
int
,
int
>
move1
(
displayBoard
board
);
// easy move
pair
<
int
,
int
>
move2
();
// hard move mutator
private:
// not used
...
...
Board.cpp
View file @
11d86453
...
...
@@ -6,6 +6,7 @@ using namespace std;
// Place starting pieces
void
displayBoard
::
start
()
{
// The rest of the board has random data in it possible including X and O
board
[
4
-
1
][
4
-
1
]
=
{
'X'
};
board
[
5
-
1
][
4
-
1
]
=
{
'O'
};
board
[
5
-
1
][
5
-
1
]
=
{
'X'
};
...
...
@@ -396,29 +397,33 @@ void displayBoard::uprightCheck(int x,int y,char chip, bool change) { // functi
// Checks to see if there is an opponents piece around the placed piece
bool
displayBoard
::
isValidAdjacent
(
int
row
,
int
col
,
char
oppTurn
)
{
if
(
board
[
row
+
1
][
col
]
==
oppTurn
)
{
bool
displayBoard
::
isValidAdjacent
(
int
x
,
int
y
,
char
oppTurn
)
{
if
(
y
==
2
){
cout
<<
"displayBoard::isValidAdjacent("
<<
x
<<
","
<<
y
<<
","
<<
(
char
)
oppTurn
<<
")"
;
cout
<<
" where (4,2) is
\'
"
<<
(
char
)
board
[
4
][
2
]
<<
"
\'
("
<<
(
int
)
board
[
4
][
2
]
<<
")"
<<
endl
;
}
if
(
board
[
x
+
1
][
y
]
==
oppTurn
)
{
return
true
;
}
else
if
(
board
[
row
-
1
][
col
]
==
oppTurn
)
{
else
if
(
board
[
x
-
1
][
y
]
==
oppTurn
)
{
return
true
;
}
else
if
(
board
[
row
][
col
+
1
]
==
oppTurn
)
{
else
if
(
board
[
x
][
y
+
1
]
==
oppTurn
)
{
return
true
;
}
else
if
(
board
[
row
][
col
-
1
]
==
oppTurn
)
{
else
if
(
board
[
x
][
y
-
1
]
==
oppTurn
)
{
return
true
;
}
else
if
(
board
[
row
-
1
][
col
-
1
]
==
oppTurn
)
{
else
if
(
board
[
x
-
1
][
y
-
1
]
==
oppTurn
)
{
return
true
;
}
else
if
(
board
[
row
-
1
][
col
+
1
]
==
oppTurn
)
{
else
if
(
board
[
x
-
1
][
y
+
1
]
==
oppTurn
)
{
return
true
;
}
else
if
(
board
[
row
+
1
][
col
-
1
]
==
oppTurn
)
{
else
if
(
board
[
x
+
1
][
y
-
1
]
==
oppTurn
)
{
return
true
;
}
else
if
(
board
[
row
+
1
][
col
+
1
]
==
oppTurn
)
{
else
if
(
board
[
x
+
1
][
y
+
1
]
==
oppTurn
)
{
return
true
;
}
return
false
;
...
...
Board.h
View file @
11d86453
#ifndef BOARD_H
#define BOARD_H
#include <iostream>
#include <vector>
using
namespace
std
;
class
displayBoard
{
...
...
@@ -38,3 +38,4 @@ class displayBoard {
};
#endif
\ No newline at end of file
mainOthello.cpp
View file @
11d86453
...
...
@@ -9,7 +9,7 @@ int main() {
int
seed
=
0
;
int
turn
=
1
;
int
difficulty
=
0
;
int
x
,
y
;
int
x
,
y
;
int
aiX
,
aiY
;
pair
<
int
,
int
>
AImove
;
bool
winner
=
false
;
...
...
@@ -52,7 +52,7 @@ int main() {
}
// start the game
displayBoard
board
;
displayBoard
board
;
// Brand new board
board
.
start
();
board
.
printBoard
();
cout
<<
"X goes first."
<<
endl
;
...
...
@@ -93,10 +93,12 @@ int main() {
// Player two's Move
}
else
if
(
turn
==
2
)
{
AImove
=
AIturn
.
move1
();
AImove
=
AIturn
.
move1
(
board
);
cout
<<
AImove
.
first
<<
" "
<<
AImove
.
second
<<
endl
;
// Check fo valid move
// if(board.isValidAdjacent(aiX, aiY, 'X') && board.isValidFlip(aiX, aiY, 'O')){
aiX
=
AImove
.
first
;
aiY
=
AImove
.
second
;
//Check fo valid move
// if(board.isValidAdjacent(aiX-1, aiY-1, 'X') && board.isValidFlip(aiX-1, aiY-1, 'O')){
// // Player moves
// board.playerMove2(aiX,aiY);
// // Check for flips in every direction
...
...
@@ -110,7 +112,7 @@ int main() {
// board.downrightCheck(aiX,aiY,'O', true);
// board.downleftCheck(aiX,aiY,'O', true);
//
Change to player 1
//Change to player 1
turn
--
;
// }else{
...
...
@@ -161,29 +163,31 @@ int main() {
}
else
if
(
turn
==
2
)
{
AImove
=
AIturn
.
move2
();
cout
<<
AImove
.
first
<<
" "
<<
AImove
.
second
<<
endl
;
aiX
=
AImove
.
first
;
aiY
=
AImove
.
second
;
// Check fo valid move
// if(board.isValidAdjacent(aiX, aiY, 'X') && board.isValidFlip(aiX, aiY
, 'O')){
//
// Player moves
//
board.playerMove2(aiX,aiY);
//
// Check for flips in every direction
//
board.upCheck(aiX,aiY, 'O', true);
//
board.downCheck(aiX,aiY, 'O', true);
//
board.leftCheck(aiX,aiY, 'O', true);
//
board.rightCheck(aiX,aiY, 'O', true);
if
(
board
.
isValidAdjacent
(
aiX
-
1
,
aiY
-
1
,
'X'
)
&&
board
.
isValidFlip
(
aiX
-
1
,
aiY
-
1
,
'O'
)){
// Player moves
board
.
playerMove2
(
aiX
,
aiY
);
// Check for flips in every direction
board
.
upCheck
(
aiX
,
aiY
,
'O'
,
true
);
board
.
downCheck
(
aiX
,
aiY
,
'O'
,
true
);
board
.
leftCheck
(
aiX
,
aiY
,
'O'
,
true
);
board
.
rightCheck
(
aiX
,
aiY
,
'O'
,
true
);
//
board.uprightCheck(aiX,aiY,'O', true);
//
board.upleftCheck(aiX,aiY,'O', true);
//
board.downrightCheck(aiX,aiY,'O', true);
//
board.downleftCheck(aiX,aiY,'O', true);
board
.
uprightCheck
(
aiX
,
aiY
,
'O'
,
true
);
board
.
upleftCheck
(
aiX
,
aiY
,
'O'
,
true
);
board
.
downrightCheck
(
aiX
,
aiY
,
'O'
,
true
);
board
.
downleftCheck
(
aiX
,
aiY
,
'O'
,
true
);
// Change to player 1
turn
--
;
//
}else{
//
// Prints invalid and then asks for another move
//
cout << "Invalid Move!"<<endl;
}
else
{
// Prints invalid and then asks for another move
cout
<<
"Invalid Move!"
<<
endl
;
//
}
}
}
board
.
printBoard
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment