Skip to content

Commit

Permalink
Merge pull request #38 from Robert3141/beta
Browse files Browse the repository at this point in the history
V3.2.2
  • Loading branch information
Robert3141 committed Dec 4, 2020
2 parents 77b9d72 + 462df96 commit 49d627a
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"shared_preferences","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences-0.5.12+4\\\\","dependencies":[]}],"android":[{"name":"shared_preferences","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences-0.5.12+4\\\\","dependencies":[]}],"macos":[{"name":"desktop_window","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\desktop_window-0.3.0\\\\","dependencies":[]},{"name":"shared_preferences_macos","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_macos-0.0.1+11\\\\","dependencies":[]}],"linux":[{"name":"desktop_window","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\desktop_window-0.3.0\\\\","dependencies":[]},{"name":"path_provider_linux","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_linux-0.0.1+2\\\\","dependencies":[]},{"name":"shared_preferences_linux","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_linux-0.0.2+4\\\\","dependencies":["path_provider_linux"]}],"windows":[{"name":"desktop_window","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\desktop_window-0.3.0\\\\","dependencies":[]},{"name":"path_provider_windows","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_windows-0.0.4+3\\\\","dependencies":[]},{"name":"shared_preferences_windows","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_windows-0.0.1+3\\\\","dependencies":["path_provider_windows"]}],"web":[{"name":"shared_preferences_web","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_web-0.1.2+7\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"desktop_window","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"shared_preferences","dependencies":["shared_preferences_linux","shared_preferences_macos","shared_preferences_web","shared_preferences_windows"]},{"name":"shared_preferences_linux","dependencies":["path_provider_linux"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]},{"name":"shared_preferences_windows","dependencies":["path_provider_windows"]}],"date_created":"2020-11-07 16:55:45.810263","version":"1.24.0-3.0.pre"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"shared_preferences","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences-0.5.12+4\\\\","dependencies":[]}],"android":[{"name":"shared_preferences","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences-0.5.12+4\\\\","dependencies":[]}],"macos":[{"name":"desktop_window","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\desktop_window-0.3.0\\\\","dependencies":[]},{"name":"shared_preferences_macos","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_macos-0.0.1+11\\\\","dependencies":[]}],"linux":[{"name":"desktop_window","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\desktop_window-0.3.0\\\\","dependencies":[]},{"name":"path_provider_linux","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_linux-0.0.1+2\\\\","dependencies":[]},{"name":"shared_preferences_linux","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_linux-0.0.2+4\\\\","dependencies":["path_provider_linux"]}],"windows":[{"name":"desktop_window","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\desktop_window-0.3.0\\\\","dependencies":[]},{"name":"path_provider_windows","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\path_provider_windows-0.0.4+3\\\\","dependencies":[]},{"name":"shared_preferences_windows","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_windows-0.0.1+3\\\\","dependencies":["path_provider_windows"]}],"web":[{"name":"shared_preferences_web","path":"C:\\\\tools\\\\flutter\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\shared_preferences_web-0.1.2+7\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"desktop_window","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"shared_preferences","dependencies":["shared_preferences_linux","shared_preferences_macos","shared_preferences_web","shared_preferences_windows"]},{"name":"shared_preferences_linux","dependencies":["path_provider_linux"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]},{"name":"shared_preferences_windows","dependencies":["path_provider_windows"]}],"date_created":"2020-12-04 13:30:59.638347","version":"1.24.0-3.0.pre"}
58 changes: 11 additions & 47 deletions lib/UI/UIgame.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:isolate';
import 'package:tinycolor/tinycolor.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -73,58 +72,20 @@ class _GamePageState extends State<GamePage> {
});
}

//code stolen from https://flutter.dev/docs/get-started/flutter-for/android-devs#how-do-you-move-work-to-a-background-thread
//create isolate and return value when done
Future isolateMinMax() async {
//create new receive port
ReceivePort receivePort = ReceivePort();
//spawn in isolate with this receive port
await Isolate.spawn(dataLoader, receivePort.sendPort);
//get send port from receive port
SendPort sendPort = await receivePort.first;
//return the value from the isolate
return await sendReceive(sendPort, 2 * globals.recursionLimit,
globals.mainBoard, globals.boardSize);
}

Future sendReceive(SendPort port, recursion, board, boardSize) {
ReceivePort receivePort = ReceivePort();
//send data in port into isolate
port.send([receivePort.sendPort, recursion, board, boardSize]);
return receivePort.first;
}

//the actual isolate routine
static dataLoader(SendPort sendPort) async {
//get the port of the isolate
ReceivePort port = ReceivePort();
//tell other isolates this is listening on this send port
sendPort.send(port.sendPort);
//get data from port
await for (var msg in port) {
//get send port for return of data
SendPort sendPort = msg[0];
//get result from minMax fun and give it data from receive port message
int result = await logic.minMax(msg[1], msg[2], msg[3], true);
//return the result from the isolate
sendPort.send(result);
}
}

Future runAI() async {
//local vars
int players = globals.amountOfPlayers;
globals.amountOfPlayers = 2;
int winner = 0;
int columnChosen = logic.randomNumber(0, globals.boardSize - 1);
;

//choose column
//Web doesn't support isolates
//run in isolate to stop main thread being cluttered so UI can still update
if (globals.recursionLimit != 0) {
if (!kIsWeb) {
columnChosen = await isolateMinMax();
columnChosen = await logic.isolateMinMax(2 * globals.recursionLimit,
globals.mainBoard, globals.boardSize, true);
} else {
columnChosen = await logic.minMax(2 * globals.recursionLimit,
globals.mainBoard, globals.boardSize, true);
Expand All @@ -141,7 +102,8 @@ class _GamePageState extends State<GamePage> {
globals.mainBoard, globals.boardSize, columnChosen);
});
//temp increase amount of players
winner = logic.checkWinner(globals.mainBoard, globals.boardSize);
winner =
logic.checkWinner(globals.mainBoard, globals.boardSize, columnChosen);
if (winner == 0 && spaceOnBoard()) {
//next player already gonna occur
} else {
Expand Down Expand Up @@ -237,7 +199,8 @@ class _GamePageState extends State<GamePage> {
globals.mainBoard, globals.boardSize, columnNumber);
}
});
winner = logic.checkWinner(globals.mainBoard, globals.boardSize);
winner = logic.checkWinner(
globals.mainBoard, globals.boardSize, columnNumber);
if (winner == 0 && spaceOnBoard()) {
await nextPlayer();
} else {
Expand Down Expand Up @@ -275,12 +238,13 @@ class _GamePageState extends State<GamePage> {
Widget build(BuildContext context) {
//local variables of size
double _paddingInsets = globals.defaultPadding / globals.boardSize;
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
double smallestLength = width > height ? height : width;
double _counterSize =
(MediaQuery.of(context).size.width - globals.defaultPadding) /
(globals.boardSize + 1);
(smallestLength - globals.defaultPadding) / (globals.boardSize + 2);
double _counterRadius =
(MediaQuery.of(context).size.width / 2 - globals.defaultPadding) /
(globals.boardSize + 1);
(smallestLength / 2 - globals.defaultPadding) / (globals.boardSize + 2);

return Scaffold(
backgroundColor: chooseBackgroundColor(),
Expand Down
104 changes: 65 additions & 39 deletions lib/globals.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
library connectron.globals;

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';

///
/// STRINGS
Expand All @@ -25,13 +26,13 @@ const int roundDefault = 1;
const int roundMax = 5;
const int recursionMin = 0;
const int recursionDefault = 2;
const int recursionMax = 2;
const int recursionMax = kIsWeb ? 2 : 3;
const List<List<int>> optionalPresetsValues = [
//FORMAT board player lineLength, roundAmount, recursionVal
[boardDefault,playerDefault],
[7,2],
[11,3],
[15,4],
[boardDefault, playerDefault],
[7, 2],
[11, 3],
[15, 4],
];
const List<String> optionalPresetsTitles = [
"1 Player",
Expand All @@ -52,27 +53,47 @@ const String lblRunGame = "Run Game";
const String errorTitleError = "Error";
const String errorActionAccept = "OK";
const String errorTitleInput = "Invalid Input";
const String errorMsgInputBoardSizeSmall = "The board size is too small. It should be $boardMin-$boardMax";
const String errorMsgInputBoardSizeLarge = "The board size is too large. It should be $boardMin-$boardMax";
const String errorMsgInputPlayerAmountSmall = "The player amount is too small. It should be $playerMin-$playerMax";
const String errorMsgInputPlayerAmountLarge = "The player amount is too large. It should be $playerMin-$playerMax";
const String errorMsgInputLineLengthSmall = "The line length is too small. It should be $lineMin-$lineMax";
const String errorMsgInputLineLengthLarge = "The line length is too large. It should be $lineMin-$lineMax";
const String errorMsgInputLineLengthProblem = "The line length is larger than the board!";
const String errorMsgInputRoundSmall = "The round number is too small. It should be $roundMin-$roundMax";
const String errorMsgInputRoundLarge = "The round number is too large. It should be $roundMin-$roundMax";
const String errorMsgInputRecursionSmall = "The CPU level is too small. It should be $recursionMin-$recursionMax";
const String errorMsgInputRecursionLarge = "The CPU level is too large. It should be $recursionMin-$recursionMax";
const String errorMsgInputBoardSizeSmall =
"The board size is too small. It should be $boardMin-$boardMax";
const String errorMsgInputBoardSizeLarge =
"The board size is too large. It should be $boardMin-$boardMax";
const String errorMsgInputPlayerAmountSmall =
"The player amount is too small. It should be $playerMin-$playerMax";
const String errorMsgInputPlayerAmountLarge =
"The player amount is too large. It should be $playerMin-$playerMax";
const String errorMsgInputLineLengthSmall =
"The line length is too small. It should be $lineMin-$lineMax";
const String errorMsgInputLineLengthLarge =
"The line length is too large. It should be $lineMin-$lineMax";
const String errorMsgInputLineLengthProblem =
"The line length is larger than the board!";
const String errorMsgInputRoundSmall =
"The round number is too small. It should be $roundMin-$roundMax";
const String errorMsgInputRoundLarge =
"The round number is too large. It should be $roundMin-$roundMax";
const String errorMsgInputRecursionSmall =
"The CPU level is too small. It should be $recursionMin-$recursionMax";
const String errorMsgInputRecursionLarge =
"The CPU level is too large. It should be $recursionMin-$recursionMax";
const String helpTitleHelp = "Help";
const String helpMsgHelpMain = "Connectron is a Connect 4 style game played by $playerMin-$playerMax. Use the presets to select a game for players or delve into the fine tuning for precise setup";
const String helpOptionalPresets = "Premade settings for the game (good for most use cases)";
const String helpBoardSize = "The board width and height (larger boards have smaller places for counters so you might have to play with the device horizontal)";
const String helpAmountOfPlayers = "The amount of players that can play (the background dictates which player's turn it is)";
const String helpLineLength = "The length of the line in any direction that needs to be formed in order for a player to be victorious";
const String helpRoundNum = "The amount of rounds that are run. After each round the board is reset. The winner of all the rounds is calculated at the end";
const String helpRecursion = "The difficulty of the CPU. As difficulty increases the CPU takes longer to calcualte an appropriate move";
const String helpBombCounter = "This is a special counter that can be used once per round/game. It can be placed by pressing the bomb icon beneath the desired row. It destroys everything around it including itself";
const String helpMsgHelpGame = "To play press a spot on the chosen column or use the arrow button. The background colour represents the player playing.";
const String helpMsgHelpMain =
"Connectron is a Connect 4 style game played by $playerMin-$playerMax. Use the presets to select a game for players or delve into the fine tuning for precise setup";
const String helpOptionalPresets =
"Premade settings for the game (good for most use cases)";
const String helpBoardSize =
"The board width and height (larger boards have smaller places for counters so you might have to play with the device horizontal)";
const String helpAmountOfPlayers =
"The amount of players that can play (the background dictates which player's turn it is)";
const String helpLineLength =
"The length of the line in any direction that needs to be formed in order for a player to be victorious";
const String helpRoundNum =
"The amount of rounds that are run. After each round the board is reset. The winner of all the rounds is calculated at the end";
const String helpRecursion =
"The difficulty of the CPU. As difficulty increases the CPU takes longer to calcualte an appropriate move";
const String helpBombCounter =
"This is a special counter that can be used once per round/game. It can be placed by pressing the bomb icon beneath the desired row. It destroys everything around it including itself";
const String helpMsgHelpGame =
"To play press a spot on the chosen column or use the arrow button. The background colour represents the player playing.";
const double defaultPadding = 12.0;
TextEditingController conBoardSize = TextEditingController();
TextEditingController conAmountOfPlayers = TextEditingController();
Expand All @@ -89,17 +110,17 @@ const outputMsgOverall = " and the overall winner is player ";
const outputMsgBoardNoSpace = "The column you selected is full!";
const lblOptions = "Options";
const List<Color> playerColors = [
Color.fromRGBO(255,255,255,1.0), //00 WHITE
Color.fromRGBO(198, 40, 40,1.0), //01 RED
Color.fromRGBO(249, 168, 37,1.0), //02 YELLOW
Color.fromRGBO(46, 125, 50,1.0), //03 GREEN
Color.fromRGBO(106, 27, 154,1.0), //04 PURPLE
Color.fromRGBO(173, 20, 87,1.0), //05 PINK
Color.fromRGBO(216, 67, 21,1.0), //06 ORANGE
Color.fromRGBO(55, 71, 79,1.0), //07 BLUE-GREY
Color.fromRGBO(21, 101, 192,1.0), //08 BLUE
Color.fromRGBO(158, 157, 36,1.0), //09 LIME
Color.fromRGBO(0, 131, 143,1.0) //10 CYAN
Color.fromRGBO(255, 255, 255, 1.0), //00 WHITE
Color.fromRGBO(198, 40, 40, 1.0), //01 RED
Color.fromRGBO(249, 168, 37, 1.0), //02 YELLOW
Color.fromRGBO(46, 125, 50, 1.0), //03 GREEN
Color.fromRGBO(106, 27, 154, 1.0), //04 PURPLE
Color.fromRGBO(173, 20, 87, 1.0), //05 PINK
Color.fromRGBO(216, 67, 21, 1.0), //06 ORANGE
Color.fromRGBO(55, 71, 79, 1.0), //07 BLUE-GREY
Color.fromRGBO(21, 101, 192, 1.0), //08 BLUE
Color.fromRGBO(158, 157, 36, 1.0), //09 LIME
Color.fromRGBO(0, 131, 143, 1.0) //10 CYAN
];
const List<String> playerNames = [
"nobody",
Expand All @@ -114,17 +135,22 @@ const List<String> playerNames = [
"lime",
"cyan",
];

int selectedPreset = 0;
bool running = false;
int boardSize = boardDefault;
int amountOfPlayers = playerDefault;
int amountOfRounds = roundDefault;
int lineLength = lineDefault;
int recursionLimit = recursionDefault;

bool recursionEnabled = true;
bool bombCounter = false;
bool running = false;

int roundNumber = roundDefault;
int playerNumber = playerDefault;

List<bool> playerBombs = new List<bool>.generate(playerDefault, (i) => true);
List<List<int>> mainBoard = new List<List<int>>.generate(boardDefault, (i) => List<int>.generate(boardDefault, (j) => 0));
List<int> playerScores = new List<int>.generate(playerDefault, (i) => 0);
List<List<int>> mainBoard = new List<List<int>>.generate(
boardDefault, (i) => List<int>.generate(boardDefault, (j) => 0));
List<int> playerScores = new List<int>.generate(playerDefault, (i) => 0);

0 comments on commit 49d627a

Please sign in to comment.