Danke für Hinweise wegen des Codes an sich, aber dass die Methode murks ist, war mir von vornerein klar. Deswegen habe ich bereits auch die vier row a-d Methoden im Code drin
Nur ärgerlich, dass ich das ganze nicht ausprobieren kann, da der Compiler sich mit der o.g. Fehlermeldung weigert das ganze zu compilieren...
Und bitte, mir ist auch klar wie genau ich die Row methode verbessern muss, damit sie vernünftige Ergebnisse produziert... Macht nur nicht sonderlich viel Spaß, wenn man sie nicht auch gleichzeitig testen kann.
Und hier nochmal die Fehlermeldung:
Nur ärgerlich, dass ich das ganze nicht ausprobieren kann, da der Compiler sich mit der o.g. Fehlermeldung weigert das ganze zu compilieren...
Und bitte, mir ist auch klar wie genau ich die Row methode verbessern muss, damit sie vernünftige Ergebnisse produziert... Macht nur nicht sonderlich viel Spaß, wenn man sie nicht auch gleichzeitig testen kann.
Code:
//Ki.h
#include "stdafx.h"
#include "spielfeld.h"
#include <stdio.h> // printf, scanf, puts, NULL
#include <stdlib.h> // srand, rand
#include <time.h> // time
using namespace::std;
class Ki
{
public:
Ki::Ki(bool);
short Ki::moverandom();
short Ki::move(Spielfeld);
short Ki::winpossible(Spielfeld);
private:
bool player;
};
Code:
//Ki.cpp
#include "stdafx.h"
#include "Ki.h"
#include "spielfeld.h"
#include <stdio.h> // printf, scanf, puts, NULL
#include <stdlib.h> // srand, rand
#include <time.h> // time
using namespace::std;
short Ki::moverandom(){
return (rand() % 6 + 1);
}
short Ki::move(Spielfeld s){
Spielfeld test = s;
test.setchainlength(3);
test.setausgabe(false);
if (winpossible(test)) return winpossible(test);
return moverandom();
}
short Ki::winpossible(Spielfeld t){
t.winner();
return 1;
}
Ki::Ki(bool p){
player = p;
}
Code:
// viergewinnt.cpp : main project file.
#include "stdafx.h"
#include "Spielfeld.h"
#include "Ki.h"
using namespace System;
int main(array<System::String ^> ^args)
{
srand(time(NULL));
Spielfeld real(7, 6, 4, true, true);
Spielfeld test(7, 6, 4, false, false);
Ki enemy(real.getplayer());
short toset = -1;
cout << "starting game" << endl;
while (real.getactive()){
if (real.getplayer())
real.setzen(enemy.move(real));
else
{
cout << "player " << real.getplayer() << "'s turn" << endl;
while (!real.setzen(toset))
{
cout << "pls enter the number between 0 and " << real.getbreite() << endl;
cin >> toset;
}
toset = -1;
}
}
char c; cin >> c;
return 0;
}
Code:
//Spielfeld.h
#pragma once
#include <iostream>
#include <vector>
#include "line.h"
#include "MyClass.h"
using namespace::std;
class Spielfeld
{
public:
//Spielfeld() : line{}
//{}
Spielfeld(int, int, short, bool, bool); // breite, hoehe, benoetigte steine in einer reihe, ausgabe, stop nach sieg
bool setzen(short);
short winner();
void undo();
short movenum();
bool getplayer();
void setplayer(bool);
bool getactive();
void setactive(bool);
void nextplayer();
short getbreite();
short gethoehe();
void ausgeben();
void setausgabe(bool);
void setchainlength(short);
void free(){
}
std::vector<line> row(short);
std::vector<line> rowa(short);
std::vector<line> rowb(short);
std::vector<line> rowc(short);
std::vector<line> rowd(short);
private:
short move = 0; // Anzahl getaetigter züge
std::vector<short> movesx;
std::vector<short> movesy;
std::vector<std::vector<short>> feld; // Spielfeld
int breite, hoehe;
bool player; // Spieler der aktuell am Zug ist
short chainlength; // Benoetigte Steine in einer Reihe um zu gewinnen
bool active, ausgabe, stoppen; // Spiel noch aktiv?, zuege ausgeben?, nach Sieg spiel beenden? (zuruecknehmen nicht mgl)
short winner(short b, short h){
// effizienterer Algorithmus, der nur Steine in der Umgebung des zuletztgesetzten überprüft
return 0;
}
};
Code:
//Spielfeld.h
#include "stdafx.h"
#include "Spielfeld.h"
#include "line.h"
Spielfeld::Spielfeld(int b, int h, short c, bool a, bool s) :
move(0),
movesx(b * h),
movesy(b * h),
feld(breite, vector<short>(h)),
breite(b),
hoehe(h),
player(0),
chainlength(c),
active(true),
ausgabe(a),
stoppen(s)
{}
bool Spielfeld::setzen(short b)
{
if (getactive() == false || b < 0 || b >= breite || feld[b][hoehe - 1] != 0)
return false;
int h = 0;
for (; (h < hoehe) & (feld[b][h] != 0); h++);
if (feld[b][h] != 0)
return false;
feld[b][h] = (getplayer() + 1);
movesx[move] = b;
movesy[move] = h;
nextplayer();
move++;
if (ausgabe)
{
system("cls");
ausgeben();
}
winner();
return true;
}
short Spielfeld::winner()
{
for (int y = 0; y < hoehe; y++){
for (int x = 0; x < breite; x++){
//cout << x << "/" << y << " tested " << endl;
if (x < breite - chainlength && feld[x][y] != 0 && feld[x][y] == feld[x + 1][y] &&
feld[x + 2][y] == feld[x + 3][y] && feld[x + 1][y] == feld[x + 2][y]) {
if (stoppen == true)
setactive(false);
if (ausgabe)
cout << "GAME ENDED (horizontal win)" << endl;
return feld[x][y];
}
if (y < hoehe - chainlength && feld[x][y] != 0 && feld[x][y] == feld[x][y + 1] &&
feld[x][y + 2] == feld[x][y + 3] && feld[x][y + 1] == feld[x][y + 2]) {
if (stoppen == true)
setactive(false);
if (ausgabe)
cout << "GAME ENDED (vertical win)" << endl;
//cout << "There is a row from " << x << "|" << y << " to " << x << "|" << (y+4) << endl;
return feld[x][y];
}
if (y < hoehe - chainlength && x < breite - chainlength && feld[x][y] != 0 && feld[x][y] == feld[x + 1][y + 1] &&
feld[x + 2][y + 2] == feld[x + 3][y + 3] && feld[x + 1][y + 1] == feld[x + 2][y + 2]) {
if (stoppen == true)
setactive(false);
if (ausgabe)
cout << "GAME ENDED (diagonal win)" << endl;
//cout << "There is a row from " << x << "|" << y << " to " << x << "|" << (y+4) << endl;
return feld[x][y];
}
if (y < hoehe - chainlength && x - (chainlength - 1) >= 0 && feld[x][y] != 0 && feld[x][y] == feld[x - 1][y + 1] &&
feld[x - 2][y + 2] == feld[x - 3][y + 3] && feld[x - 1][y + 1] == feld[x - 2][y + 2]) {
if (stoppen == true)
setactive(false);
if (ausgabe)
cout << "GAME ENDED (diagonal win)" << endl;
//cout << "There is a row from " << x << "|" << y << " to " << x << "|" << (y-4) << endl;
return feld[x][y];
}
}
}
return 0;
}
std::vector<line> Spielfeld::rowa(short searchedlength) //done
{
line nline;
point a, b;
vector<line> vec;
vec.reserve(20);
int vecnum = 0;
for (int y = 0; y < hoehe; y++)
{
for (int x = 0; x < breite; x++)
{
if (x < breite && feld[x][y] != 0 && feld[x][y] == feld[x + 1][y])
{
if (nline.end().x() == x && nline.end().y() == y) // geht die reihe weiter?
{
b.set(x + 1, y);
nline.set(a, b); // reihe verlängern
}
else
{
vec.push_back(nline); //alte reihe abspeichern
vecnum++;
a.set(x, y);
b.set(x + 1, y);
nline.set(a, b); // neue reihe erstellen
}
}
}
}
}
vector<line> Spielfeld::rowb(short searchedlength)
{
line nline;
point a, b;
vector<line> vec;
vec.reserve(20);
int vecnum = 0;
int x, y;
for (int xbackup = breite - 1; xbackup > 0; xbackup--)
{
for (int ybackup = 0; ybackup < hoehe; ybackup++)
{
for (x = xbackup, y = ybackup; x < breite && y < hoehe && (feld[x][y] != 0) & (feld[x][y] == feld[x + 1][y + 1]); x++, y++)
{
if (nline.end().x() == x && nline.end().y() == y) // geht die reihe weiter?
{
b.set(x + 1, y + 1);
nline.set(a, b); // reihe verlängern
}
else
{
vec.push_back(nline); //alte reihe abspeichern
vecnum++;
a.set(x, y);
b.set(x + 1, y + 1);
nline.set(a, b); // neue reihe erstellen
}
}
}
}
}
vector<line> Spielfeld::rowc(short searchedlength) // done
{
line nline;
point a, b;
vector<line> vec;
vec.reserve(20);
int vecnum = 0;
for (int x = 0; x < breite; x++)
{
for (int y = 0; y < hoehe; y++)
{
if (y < hoehe - searchedlength && feld[x][y] != 0 && feld[x][y] == feld[x][y + 1])
{
if (nline.end().x() == x && nline.end().y() == y) // geht die reihe weiter?
{
b.set(x, y + 1);
nline.set(a, b); // reihe verlängern
}
else
{
vec.push_back(nline); //alte reihe abspeichern
vecnum++;
a.set(x, y);
b.set(x, y + 1);
nline.set(a, b); // neue reihe erstellen
}
}
}
}
}
vector<line> Spielfeld::rowd(short searchedlength)
{
line nline;
point a, b;
vector<line> vec;
vec.reserve(20);
int vecnum = 0;
int x, y;
for (int ybackup = 0; ybackup < hoehe; ybackup++)
{
for (int xbackup = 0; xbackup < breite - 1; xbackup++)
{
for (x = xbackup, y = ybackup; x < breite && y < hoehe && (feld[x][y] != 0) & (feld[x][y] == feld[x - 1][y + 1]); x--, y++)
{
if (nline.end().x() == x && nline.end().y() == y) // geht die reihe weiter?
{
b.set(x - 1, y + 1);
nline.set(a, b); // reihe verlängern
}
else
{
vec.push_back(nline); //alte reihe abspeichern
vecnum++;
a.set(x, y);
b.set(x - 1, y + 1);
nline.set(a, b); // neue reihe erstellen
}
}
}
}
}
void Spielfeld::undo()
{
move--;
feld[movesx[move]][movesy[move]] = 0;
movesx[move] = 0;
movesy[move] = 0;
nextplayer();
}
short Spielfeld::movenum()
{
return move;
}
bool Spielfeld::getplayer()
{
return player;
}
void Spielfeld::setplayer(bool b)
{
player = b;
}
bool Spielfeld::getactive()
{
return active;
}
void Spielfeld::setactive(bool b)
{
active = b;
}
void Spielfeld::nextplayer()
{
player = !player;
}
short Spielfeld::getbreite()
{
return breite;
}
short Spielfeld::gethoehe()
{
return hoehe;
}
void Spielfeld::ausgeben()
{
for (int h = hoehe - 1; h >= 0; h--){
for (int b = 0; b < breite; b++)
cout << feld[b][h] << " ";
cout << endl;
}
cout << endl;
}
void Spielfeld::setausgabe(bool b)
{
ausgabe = b;
}
void Spielfeld::setchainlength(short c)
{
chainlength = c;
}
Code:
#pragma once
#include "point.h"
class line
{
public:
line(point, point);
line();
line(line&);
void set(point, point);
point end();
point start();
private:
short m_length;
point m_start;
point m_end;
};
Code:
#include "stdafx.h"
#include "line.h"
#include "point.h"
line::line(point startpoint, point endpoint)
{
m_start = startpoint;
m_end = endpoint;
m_length = (m_start.x() == m_end.x()) ? (m_start.y() - m_end.y()) : (m_start.x() - m_end.x());
}
line::line()
{
m_start = point();
m_end = point();
m_length = 0;
}
line::line(line& l)
{
m_start = l.m_start;
m_end = l.m_end;
m_length = l.m_length;
}
void line::set(point a, point b)
{
m_start = a;
m_end = b;
m_length = (m_start.x() == m_end.x()) ? (m_start.y() - m_end.y()) : (m_start.x() - m_end.x());
}
point line::end()
{
return m_end;
}
point line::start()
{
return m_start;
}
Code:
#pragma once
class point
{
public:
point();
point(point&);
point(short, short);
short x();
short y();
void copy(point);
void set(short, short);
void setx(short);
void sety(short);
private:
short m_x, m_y;
};
Code:
#include "stdafx.h"
#include "point.h"
point::point(){
m_x = 0;
m_y = 0;
}
point::point(short x, short y)
{
m_x = x;
this->m_y = y;
}
point::point(point& p)
{
m_x = p.x();
m_y = p.y();
}
void point::set(short newx, short newy)
{
setx(newx);
sety(newy);
}
void point::setx(short newx)
{
this->m_x = newx;
}
void point::sety(short newy)
{
this->m_y = newy;
}
void point::copy(point p)
{
setx(p.x());
sety(p.y());
}
short point::x(){
return this->m_x;
}
short point::y(){
return m_y;
}
Und hier nochmal die Fehlermeldung:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(593): error C2558: class 'line' : no copy constructor available or copy constructor is declared 'explicit'
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(592) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'
1> with
1> [
1> _Ty=line
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled
1> with
1> [
1> _Ty=line
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\type_traits(572) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=line
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being compiled
1> with
1> [
1> _Alloc=std::allocator<line>
1> ]
1> spielfeld.cpp(89) : see reference to class template instantiation 'std::vector<line,std::allocator<_Ty>>' being compiled
1> with
1> [
1> _Ty=line
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(592) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'
1> with
1> [
1> _Ty=line
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled
1> with
1> [
1> _Ty=line
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\type_traits(572) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=line
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being compiled
1> with
1> [
1> _Alloc=std::allocator<line>
1> ]
1> spielfeld.cpp(89) : see reference to class template instantiation 'std::vector<line,std::allocator<_Ty>>' being compiled
1> with
1> [
1> _Ty=line
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Zuletzt bearbeitet: