Upload files to "/"

This commit is contained in:
lakhia13 2024-11-20 02:44:18 +00:00
parent 17a654ea37
commit 517718e657
5 changed files with 268 additions and 0 deletions

103
points.cxx Normal file

@ -0,0 +1,103 @@
#include "points.h"
#include <iostream>
Points::Points() {
size = 0;
capacity = 1;
array = new point[capacity];
}
Points::Points(int rows, int cols): size(0), capacity(4) {
array = new point[capacity];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if((i%2==0 && j%2!=0) || (i%2!=0 && j%2==0)) {
point temp;
temp.x = i;
temp.y = j;
push(temp);
}
}
}
}
Points::~Points() {
delete[] array;
}
int Points::getSize() {
return size;
}
int Points::getCapacity() {
return capacity;
}
void Points::push(point val) {
if (size == capacity) {
growArray();
}
array[size] = val;
size++;
}
void Points::removeVal(point val) {
for (int i = 0; i < size; i++) {
if (array[i].x==val.x && array[i].y==val.y) {
if (i==size-1) {
array[size-1].x = -1;
array[size-1].y = -1;
size--;
} else {
array[i] = array[i+1];
}
}
}
if (size == capacity/2) {
shrinkArray();
}
}
void Points::removeIndex(int index) {
for (int i = index; i < size; i++) {
if (i==size-1) {
array[size-1].x = -1;
array[size-1].x = -1;
} else {
array[i] = array[i+1];
}
}
size--;
if (size == capacity/2) {
shrinkArray();
}
}
void Points::growArray() {
capacity = capacity * 2;
point* temp = new point[capacity];
for (int i = 0; i < size; i++) {
temp[i] = array[i];
}
delete[] array;
array = temp;
}
void Points::shrinkArray() {
capacity = capacity / 2;
point* temp = new point[capacity];
for (int i = 0; i < size; i++) {
temp[i] = array[i];
}
delete[] array;
array = temp;
}
void Points::printArray() {
for (int i=0;i<size;i++) {
std::cout << array[i].x << " " << array[i].y << std::endl;
}
}

48
points.h Normal file

@ -0,0 +1,48 @@
//
// Created by pranshav on 11/18/24.
//
#ifndef POINTS_H_INCLUDED
#define POINTS_H_INCLUDED
struct point {
int x;
int y;
char player = '\0';
};
class Points {
private:
point* array = nullptr;
int size;
int capacity;
public:
Points();
Points(int rows, int cols);
~Points();
int getSize();
int getCapacity();
void push(point val);
void removeVal(point val);
void removeIndex(int index);
void growArray();
void shrinkArray();
void printArray();
friend class RandomPlayer;
friend class StrategicPlayer;
};
#endif //POINTS_H

17
randomplayer.cxx Normal file

@ -0,0 +1,17 @@
//
// Created by pranshav on 11/18/24.
//
#include "randomplayer.h"
point RandomPlayer::randomMove(Points& points) {
int size = points.getSize();
if (size != 0) {
srand(time(0));
int randomIndex = rand()%size;
point temp = points.array[randomIndex];
points.removeIndex(randomIndex);
return temp;
}
}

12
randomplayer.h Normal file

@ -0,0 +1,12 @@
//
// Created by pranshav on 11/18/24.
//
#include "points.h"
#include <cstdlib>
#include <ctime>
class RandomPlayer {
public:
point randomMove(Points& points);
};

88
scores.cxx Normal file

@ -0,0 +1,88 @@
//
// Created by pranshav on 11/18/24.
//
#include "scores.h"
Scores::Scores() : size(0), capacity(4) {
array = new ScoreNode[capacity];
}
Scores::~Scores() {
delete[] array;
}
void Scores::addScore(char player) {
for (int i = 0; i < size; i++) {
if (array[i].player == player) {
return;
}
}
if (size == capacity) {
growArray();
}
array[size].player = player;
array[size].score = 0;
size++;
}
void Scores::updateScore(char player) {
for (int i = 0; i < size; i++) {
if (array[i].player == player) {
array[i].score++;
return;
}
}
}
ScoreNode Scores::findPlayerScore(char player) {
for (int i = 0; i < size; i++) {
if (array[i].player == player) {
return array[i];
}
}
}
void Scores::printResults() {
// Sort players alphabetically
std::sort(array, array + size, [](const ScoreNode& a, const ScoreNode& b) {
return a.player < b.player;
});
// Find max score
int maxScore = 0;
for (int i = 0; i < size; i++) {
if (array[i].score > maxScore) {
maxScore = array[i].score;
}
}
// Count players with max score
int countMaxScore = 0;
for (int i = 0; i < size; i++) {
if (array[i].score == maxScore) {
countMaxScore++;
}
}
// Print results
for (int i = 0; i < size; i++) {
std::cout << "Player " << array[i].player << " has " << array[i].score;
if (array[i].score == maxScore) {
std::cout << (countMaxScore > 1 ? " (tie)" : " (win)");
}
std::cout << "." << std::endl;
}
}
void Scores::growArray() {
capacity *= 2;
ScoreNode* temp = new ScoreNode[capacity];
for (int i = 0; i < size; i++) {
temp[i] = array[i];
}
delete[] array;
array = temp;
}