Initial Commit

This commit is contained in:
plane000
2018-05-11 19:48:30 +01:00
parent 1c6553bb4f
commit b09a2dfdb2
10 changed files with 1075 additions and 0 deletions

View File

@@ -0,0 +1,152 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_Games.Ascii_Showdown {
class AsciiShowdown_AI {
//you can have it fire a horizontal line of bullets, or fire every third step, or fire a bunch in a straight line
//give it shooting patterns that it can cycle through based on situation
List<int> lastDirection = new List<int>(); //0 left, 1 right
List<int> lastMode = new List<int>(); //last movetype stored in last index
Random rnd = new Random();
int type = 0;
int iterationsOfType = 0;
bool lineOfSight = true;
int lastKnownPos = 0;
public int cX { get; set; }
public int cY { get; set; }
public char[,] moveCPU(char[,] grid, int width, int uX, int uY, bool boost) {
if (grid[uX, uY - 2] == '█') { //checks if user is hiding
uX = lastKnownPos;
lineOfSight = false;
} else {
lineOfSight = true;
}
type = getAItype(grid, width, uX, uY, boost);
if (type == 0) {
return grid;
} else if (type == 1) { //defensive, retreat and shoot every 8th
if (iterationsOfType % 8 == 0) {
grid[cX, cY + 1] = '▼';
}
if (iterationsOfType % 4 == 0) {
if (cX < uX) {
grid = moveRight(grid, width);
} else {
grid = moveLeft(grid, width);
}
}
} else if (type == 2) { //aggressive
} else if (type == 3) { //try to break walls
} else if (type == 4) { //try to block off player
} else if (type == 5) { //retreat and not shoot
if (iterationsOfType % 2 == 0) {
if (cX < 3) {
grid = moveRight(grid, width);
} else if (cX > width - 4) {
grid = moveLeft(grid, width);
} else if (cX > uX) {
grid = moveRight(grid, width);
} else {
grid = moveLeft(grid, width);
}
}
} else if (type == 6) { //blanket shot
}
iterationsOfType++;
return grid;
}
private int getAItype(char[,] grid, int width, int uX, int uY, bool boost) {
if (lastMode.Count > 1 && lastMode[lastMode.Count - 1] != lastMode[lastMode.Count - 2]) {
iterationsOfType = 0;
}
if (!lineOfSight) {
wonder(grid, width);
lastMode.Add(0); //wander
return 0;
}
if (boost) {
lastMode.Add(5); //retreat and not shoot
return 5;
}
lastMode.Add(1);
return 1;
}
private char[,] wonder(char[,] grid, int width) {
if (iterationsOfType % 4 == 0) {
int shoot = rnd.Next(1, 5);
int move = rnd.Next(0, 2);
if (shoot == 1) {
grid[cX, cY + 1] = '▼';
}
if (move == 1) {
grid = moveLeft(grid, width);
} else {
grid = moveRight(grid, width);
}
}
iterationsOfType++;
return grid;
}
private char[,] moveLeft(char[,] grid, int width) {
if (cX - 1 > 0) {
lastDirection.Add(0);
grid[cX, cY] = ' ';
cX--;
grid[cX, cY] = '☻';
}
return grid;
}
private char[,] moveRight(char[,] grid, int width) {
if (cX + 1 < width - 1) {
lastDirection.Add(1);
grid[cX, cY] = ' ';
cX++;
grid[cX, cY] = '☻';
}
return grid;
}
public void dispose() {
lastDirection.Clear();
}
/* string lastdir = "";
for (int i = 0; i < lastDirection.Count; i++) {
lastdir += lastDirection[i];
}
string lastmode = "";
for (int i = 0; i < lastMode.Count; i++) {
lastmode += lastMode[i];
}
Console.SetCursorPosition(0, 22);
Console.WriteLine("Last direction: " + lastdir);
Console.WriteLine("Last mode: " + lastmode);
Console.WriteLine("Iterations of mode: " + iterationsOfType); */
}
}

View File

@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_Games.Ascii_Showdown {
class AsciiShowdown_Draw {
public int width { get; set; }
public int height { get; set; }
public int indent { get; set; }
public void drawGrid(char[,] grid, int indent) {
try {
Console.SetCursorPosition(indent, indent - 3);
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
if (grid[i, j] == '☺') {
Console.ForegroundColor = ConsoleColor.Green;
} else if (grid[i, j] == '☻') {
Console.ForegroundColor = ConsoleColor.Red;
} else {
Console.ForegroundColor = ConsoleColor.White;
}
Console.Write(grid[i, j]);
}
Console.WriteLine();
Console.SetCursorPosition(indent, j + indent - 2);
}
} catch { }
}
public void drawStats(int chealth, int uhealth, int bullets, int walls, bool boost) {
try {
int drawX = width + indent + 10;
Console.SetCursorPosition(drawX, 6);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("╔════════════════╗");
Console.SetCursorPosition(drawX, 7);
if (chealth > 9) {
Console.WriteLine("║CPU healh: {0} ║", chealth);
} else {
Console.WriteLine("║CPU healh: {0} ║", chealth);
}
Console.SetCursorPosition(drawX, 8);
Console.WriteLine("╚════════════════╝");
Console.SetCursorPosition(drawX, 14);
Console.WriteLine("╔════════════════╗");
Console.SetCursorPosition(drawX, 15);
if (uhealth > 9) {
Console.WriteLine("║Health: {0} ║", uhealth);
} else {
Console.WriteLine("║Health: {0} ║", uhealth);
}
Console.SetCursorPosition(drawX, 16);
if (bullets > 99) {
Console.WriteLine("║Bullets: {0} ║", bullets);
} else if (bullets > 9) {
Console.WriteLine("║Bullets: {0} ║", bullets);
} else {
Console.WriteLine("║Bullets: {0} ║", bullets);
}
Console.SetCursorPosition(drawX, 17);
if (walls > 9) {
Console.WriteLine("║Walls: {0} ║", walls);
} else {
Console.WriteLine("║Walls: {0} ║", walls);
}
Console.SetCursorPosition(drawX, 18);
if (boost) {
Console.Write("║Boost: ");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("ON");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" ║");
Console.WriteLine();
} else {
Console.Write("║Boost: ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("OFF");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" ║");
Console.WriteLine();
}
Console.SetCursorPosition(drawX, 19);
Console.WriteLine("╚════════════════╝");
} catch { }
}
public void updateGrid(char[,] grid, char[,] oldGrid, int indent) {
try {
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
if (grid[i, j] != oldGrid[i, j]) {
if (grid[i, j] == '☺') {
Console.ForegroundColor = ConsoleColor.Green;
} else if (grid[i, j] == '☻') {
Console.ForegroundColor = ConsoleColor.Red;
} else {
Console.ForegroundColor = ConsoleColor.White;
}
Console.SetCursorPosition(i + indent, j + indent - 3);
Console.Write(grid[i, j]);
}
}
}
} catch { }
}
public void updateStats(int chealth, int uhealth, int bullets, int walls, bool boost) {
int drawX = width + indent + 10;
}
}
}

View File

@@ -0,0 +1,407 @@
using System;
using System.Collections.Generic;
using System.Threading;
namespace Console_Games {
class AsciiShowdown_Game {
//Dictionary<Point, char> dic = new Dictionary<Point, char>();
Random rnd = new Random();
Ascii_Showdown.AsciiShowdown_Draw draw = new Ascii_Showdown.AsciiShowdown_Draw();
Ascii_Showdown.AsciiShowdown_AI ai = new Ascii_Showdown.AsciiShowdown_AI();
int uhealth = 10, chealth = 30, bullets = 500, walls = 20;
bool boost = false;
int boostime = 100, cooldown = 200, boostimeon = 0, cooldowntime = 200;
int width = 40;
int height = 20;
int indent = 5;
int uX;
int uY;
public int cX;
public int cY;
bool HAULT = false;
int WINNER = 0;
char[,] grid;
char[,] oldGrid;
static readonly string defaultGrid =
"████████████████████████████████████████" + // █: wall
"█ ☻ █" + // ☻: cpu
"█ █" + // ☺: user
"█ █" + // ▲: user's bullet
"█ █" + // ▼: cpu's bullet
"█ █" + //boomerang(array): boomerang
"█ █" + //wall(array): user's wall
"█ █" +
"█ █" +
"█ █" +
"█ █" +
"█ █" +
"█ █" +
"█ █" +
"█ █" +
"█ █" +
"█ █" +
"█ █" +
"█ ☺ █" +
"████████████████████████████████████████";
public void initializeGame() {
Console.Clear();
Console.SetWindowSize(80, 30);
HAULT = false;
dispose();
draw.width = width;
draw.height = height;
draw.indent = indent;
grid = new char[width, height];
bool boost = false;
grid = stringToArray(defaultGrid);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (grid[i, j] == '☺') {
uX = i;
uY = j;
} else if (grid[i, j] == '☻') {
cX = i;
cY = j;
}
}
}
Console.WriteLine("The game has successfully loaded!");
Console.WriteLine("To play:");
Console.WriteLine("W or up arrow to shoot");
Console.WriteLine("A or left arrow to move left");
Console.WriteLine("D or right arrow to move right");
Console.WriteLine("O to use your boost powerup this will only last for 5 seconds with a 10 second cooldown");
Console.WriteLine("P to place a wall this will only be able to survive 4 enemy or freindly shots before breaking");
Console.WriteLine("Press Q to Quit at any time");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Console.Clear();
draw.drawGrid(grid, indent);
draw.drawStats(chealth, uhealth, bullets, walls, boost);
gameLoop();
}
private void gameLoop() {
while (WINNER == 0 && HAULT == false) {
oldGrid = getOldGrid(grid);
if (boost) {
boostimeon++;
if (boostimeon == boostime) {
boost = false;
boostimeon = 0;
cooldown = 0;
}
} else if (cooldown < cooldowntime) {
cooldown++;
}
List<ConsoleKeyInfo> keys = new List<ConsoleKeyInfo>();
while (Console.KeyAvailable) {
var key = Console.ReadKey(true);
keys.Add(key);
}
moveColideandStuff();
ai.cX = cX;
ai.cY = cY;
grid = ai.moveCPU(grid, width, uX, uY, boost);
cX = ai.cX;
cY = ai.cY;
keyPressed(keys);
//game logic calls
if (bullets == 0 || uhealth == 0) {
WINNER = 2;
break;
} else if (chealth == 0) {
WINNER = 1;
break;
}
CheckAndResetWindowSize();
draw.updateGrid(grid, oldGrid, indent);
draw.drawStats(chealth, uhealth, bullets, walls, boost);
Thread.Sleep(50);
}
if (WINNER == 1) {
Console.Clear();
Console.WriteLine("You win!");
Console.WriteLine("Press any key to continue");
Console.ReadKey();
initializeGame();
} else if (WINNER == 2) {
Console.Clear();
Console.WriteLine("You loose :(");
Console.WriteLine("Press any key to continue");
Console.ReadKey();
initializeGame();
}
dispose();
gameLoop();
}
private void keyPressed(List<ConsoleKeyInfo> keys) {
try {
if (keys.Count >= 1) {
var key = keys[keys.Count - 1];
switch (key.KeyChar) {
case 'W':
case 'w':
if (boost) {
grid[uX - 1, uY - 1] = '▲';
grid[uX, uY - 1] = '▲';
grid[uX + 1, uY - 1] = '▲';
bullets += -3;
} else {
grid[uX, uY - 1] = '▲';
bullets--;
}
break;
case 'D':
case 'd':
if (uX + 1 < width - 1) {
grid[uX, uY] = ' ';
grid[uX + 1, uY] = '☺';
uX++;
}
break;
case 'A':
case 'a':
if (uX - 1 > 0) {
grid[uX, uY] = ' ';
grid[uX - 1, uY] = '☺';
uX--;
}
break;
case 'P':
case 'p':
if (walls > 0 && grid[uX, uY - 2] != '█') {
grid[uX, uY - 2] = '█';
walls--;
}
break;
case 'O':
case 'o':
if (boost) {
boost = false;
boostimeon = 0;
} else if (!boost && cooldown == cooldowntime) {
cooldown = 0;
boost = true;
}
break;
case 'Q':
case 'q':
dispose();
HAULT = true;
UserInterface ui = new UserInterface();
ui.programStart();
break;
}
switch (key.Key) {
case ConsoleKey.UpArrow:
if (boost) {
grid[uX - 1, uY - 1] = '▲';
grid[uX, uY - 1] = '▲';
grid[uX + 1, uY - 1] = '▲';
bullets += -3;
} else {
grid[uX, uY - 1] = '▲';
bullets--;
}
break;
case ConsoleKey.RightArrow:
if (uX + 1 < width - 1) {
grid[uX, uY] = ' ';
grid[uX + 1, uY] = '☺';
uX++;
}
break;
case ConsoleKey.LeftArrow:
if (uX - 1 > 0) {
grid[uX, uY] = ' ';
grid[uX - 1, uY] = '☺';
uX--;
}
break;
case ConsoleKey.Spacebar:
if (boost) {
grid[uX - 1, uY - 1] = '▲';
grid[uX, uY - 1] = '▲';
grid[uX + 1, uY - 1] = '▲';
bullets += -3;
} else {
grid[uX, uY - 1] = '▲';
bullets--;
}
break;
}
}
} catch { }
}
private void moveColideandStuff() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (grid[i, j] == '▲') { //if the target is a user bullet
if (j > 1) { //if the target is not on a border
if (grid[i, j - 1] == '█' && j > 1) { //if the target has hit a wall which isnt the border, damage wall
grid[i, j] = ' ';
grid[i, j - 1] = '▓';
} else if (grid[i, j - 1] == '▓') { //wall dameging
grid[i, j] = ' ';
grid[i, j - 1] = '▒';
} else if (grid[i, j - 1] == '▒') {
grid[i, j] = ' ';
grid[i, j - 1] = '░';
} else if (grid[i, j - 1] == '░') {
grid[i, j] = ' ';
grid[i, j - 1] = '*';
} else if (grid[i, j - 1] == '☻') { //if the target has hit the CPU
chealth--;
grid[i, j] = ' ';
flashScreenEnemyHit();
} else { //if else, move forward
grid[i, j] = ' ';
grid[i, j - 1] = '▲';
}
} else { //if the target is on a wall, destroy it
grid[i, j] = ' ';
}
} else if (grid[i, j] == '▼') { //if the target is a CPU bullet
if (j < height - 2) { //if the target is not on a lower border
if (grid[i, j + 1] == '█' && j > 1) { //if the target has hit a wall which isnt the border, damage wall
grid[i, j] = ' ';
grid[i, j + 1] = '▓';
} else if (grid[i, j + 1] == '▓') { //wall dameging
grid[i, j] = ' ';
grid[i, j + 1] = '▒';
} else if (grid[i, j + 1] == '▒') {
grid[i, j] = ' ';
grid[i, j + 1] = '░';
} else if (grid[i, j + 1] == '░') {
grid[i, j] = ' ';
grid[i, j + 1] = '*';
} else if (grid[i, j + 1] == '☺') { //if the target has hit the user
uhealth--;
grid[i, j] = ' ';
flashScreenPlayerHit();
} else { //if else, move forward
grid[i, j] = ' ';
grid[i, j + 1] = 'v';
}
} else { //if the target is on a wall, destroy it
grid[i, j] = ' ';
}
} else if (grid[i, j] == '*') { //if its an explosion, delete it
grid[i, j] = ' ';
}
}
}
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (grid[i, j] == 'v') {
grid[i, j] = '▼';
}
}
}
}
private char[,] stringToArray(string toload) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
grid[j, i] = toload[i * width + j];
}
}
return grid;
}
private char[,] getOldGrid(char[,] currentGrid) {
char[,] oldGrid = new char[width, height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
oldGrid[i, j] = currentGrid[i, j];
}
}
return oldGrid;
}
private void flashScreenEnemyHit() {
Console.Clear();
Console.BackgroundColor = ConsoleColor.Red;
Console.Clear();
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
draw.drawGrid(grid, indent);
draw.drawStats(chealth, uhealth, bullets, walls, boost);
}
private void flashScreenPlayerHit() {
Console.Clear();
Console.BackgroundColor = ConsoleColor.Green;
Console.Clear();
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
draw.drawGrid(grid, indent);
draw.drawStats(chealth, uhealth, bullets, walls, boost);
}
private void CheckAndResetWindowSize() {
try {
if (Console.WindowWidth != 80 || Console.WindowHeight != 30) {
Console.SetWindowSize(80, 30);
Console.Clear();
draw.drawGrid(grid, indent);
draw.drawStats(chealth, uhealth, bullets, walls, boost);
}
} catch { }
}
public void dispose() {
ai.dispose();
uhealth = 10;
chealth = 30;
bullets = 500;
walls = 20;
boost = false;
boostime = 100;
cooldown = 200;
boostimeon = 0;
cooldowntime = 200;
width = 40;
height = 20;
indent = 5;
HAULT = false;
WINNER = 0;
}
}
}