C++ KI Erstellen

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.

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 ==========
 
Zuletzt bearbeitet:
Der Fehler ist, daß deine line-Klasse keinen kanonischen Kopierkonstruktor hat. Korrekt wäre:

Code:
line(const line&);

aber deiner hat die Form

Code:
line(line&);
Ergänzung ()

Gleiches Spiel übrigens auch für deine point-Klasse und eventuell noch andere Klassen ... habe jetzt nicht all deine Klassen kontrolliert.
Ergänzung ()

Deine Spielfeld-Klasse hat mehrere Methoden, die laut ihrer Signatur einen std::vector< line > zurückgeben, deren Rumpf aber nirgends ein return-Statement enthält. Wie hast du dir das vorgestellt?
 
@antred
Vielen Dank, das hatte ich zwischendurch rausgenommen, weil es im zusammenhang mit ref probleme gemacht hat.
Die habe ich aber letzten endes onehin nicht nutzen können, nur dann leider vergessen das const wieder hinzuzufügen.

Bei denen bin ich noch nicht fertig, ich hatte nur wenig nerven dran weiterzubasteln ohne sie testen zu können.
Ich möchte das ganze noch so überarbeiten, dass aus dem vector array letzten endes nur die geraden mit der länge n zurückgegeben werden.

@Miuwa: Auch wenn sie funktioniert haben, scheint der Compiler das alleine ja recht erfolgreich zu schaffen. Ich habe sie daher mal rausgenommen, ich muss ja nicht auf Teufel komm raus alles doppelt machen.
Hatte sie anfangs nur erstellt weil wir beim Studium durchgängig eigene schreiben sollten, mit der Begründung, dass die vom Compiler erstellen nicht zuverlässig funktionieren würden.
Aber gut, wahr letzten endes ja auch Java.

Edit: So, ich setzte jetzt durchgängig auf shorts, da ich mir keinen Fall vorstellen kann, wo ein Overflow möglich ist.
 
Zuletzt bearbeitet:
ZuseZ3 schrieb:
Wie sieht es denn mit den shorts genauer aus, warum könnte ich da überhaupt warnungen bekommen können?
Sorry, hab nicht daran gedacht, dass ich immer mit /W4 kompiliere - das produziert mehr Warnungen als die Standardeinstellung (/W3?). Warnungen gibt es deswegen, weil höhe und breite ints sind, aber z.B. getbreite() ein short zurück gibt. Theoretisch könnte es hier also zu Fehlern kommen, wenn du Spielfelder > 2^15 hast ;). Ein ähnliches "Problem" gibt es in der setzen Funktion wo h ein int ist, der dann aber einem Eintrag in movesy zugewiesen wird.
Ich würds halt zumindest einheitlich gestalten: alles short oder alles int.

Es ist in diesem Fall übrigens unnötig einen eigenen Copy/Move Konstruktor/ assignment Operator zu definieren. Die vom Kompiler generierten Funktionen tun genau das Richtige.
 
Zuletzt bearbeitet:
Meine neue Spielfeld.cpp datei, mitlerweile läuft alles wie gewünscht :)
Code:
//Spielfeld.cpp
#include "stdafx.h"
#include "Spielfeld.h"
#include "line.h"

Spielfeld::Spielfeld(short b, short h, short c, bool a, bool s)
{
	breite = b; hoehe = h; player = 0; chainlength = c, ausgabe = a, stoppen = s;
	feld.resize(breite, vector<short>(hoehe));
	movesx.resize(breite * hoehe);
	movesy.resize(breite * hoehe);
	active = true;
}


bool Spielfeld::setzen(short b)
{
	if (getactive() == false || b < 0 || b >= breite || feld[b][hoehe - 1] != 0) 
		return false;
	short 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;
}

vector<vector<line>> Spielfeld::row(short searchedlength)
{
	short sl = searchedlength;
	vector<vector<line>> twodimres;
	vector<line> res;

	res = rowa(sl);
	if ((res.size() != 0) && (res[0].getlength() == searchedlength))
	{
		if (ausgabe && active)
			res[0].output(); 
		twodimres.push_back(res);
	}

	res = rowb(sl);
	if ((res.size() != 0) && (res[0].getlength() == searchedlength))
	{
		if (ausgabe && active)
			res[0].output();
		twodimres.push_back(res);
	}

	res = rowc(sl);
	if ((res.size() != 0) && (res[0].getlength() == searchedlength))
	{
		if (ausgabe && active)
			res[0].output();
		twodimres.push_back(res);
	}

	res = rowd(sl);
	if ((res.size() != 0) && (res[0].getlength() == searchedlength))
	{
		if (ausgabe && active)
			res[0].output();
		twodimres.push_back(res);
	}
	return twodimres;
}

vector<line> Spielfeld::rowa(short searchedlength) //done
{
	line nline;
	point a, b;
	vector<line> vec;
	vec.reserve(1000);
	short vecnum = 0;
	for (short y = 0; y < hoehe; y++)
	{
		for (short x = 0; x < breite; x++)
		{
			if ((x < breite - 1) && (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
				{
					if (nline.getlength() == searchedlength)
						vec.push_back(nline); //alte reihe abspeichern
					vecnum++;
					a.set(x, y);
					b.set(x + 1, y);
					nline.set(a, b); // neue reihe erstellen
				}
			}
		}
	}
	if (nline.getlength() == searchedlength)
		vec.push_back(nline);
	return vec;
}

vector<line> Spielfeld::rowb(short searchedlength)
{
	line nline;
	point a, b;
	vector<line> vec;
	vec.reserve(1000);
	short vecnum = 0;
	short x, y;
	for (short xbackup = breite - 1; xbackup > 0; xbackup--)
	{
		for (short ybackup = 0; ybackup < hoehe; ybackup++)
		{
			for (x = xbackup, y = ybackup; x < breite - 1 && y < hoehe - 1 && (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
				{
					if (nline.getlength() == searchedlength)
						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
				}
			}
		}
	}
	if (nline.getlength() == searchedlength)
		vec.push_back(nline);
	return vec;
}

vector<line> Spielfeld::rowc(short searchedlength) // done
{
	line nline;
	point a, b;
	vector<line> vec;
	vec.reserve(1000);
	short vecnum = 0;
	for (short x = 0; x < breite; x++)
	{
		for (short y = 0; y < hoehe; y++)
		{
			if (y < hoehe - 1 && 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
				{
					if (nline.getlength() == searchedlength)
						vec.push_back(nline); //alte reihe abspeichern
					vecnum++;
					a.set(x, y);
					b.set(x, y + 1);
					nline.set(a, b); // neue reihe erstellen
				}
			}
		}
	}
	if (nline.getlength() == searchedlength)
		vec.push_back(nline);
	return vec;
}

vector<line> Spielfeld::rowd(short searchedlength)
{
	line nline;
	point a, b;
	vector<line> vec;
	vec.reserve(1000);
	short vecnum = 0;
	short x, y;
	for (short ybackup = 0; ybackup < hoehe; ybackup++)
	{
		for (short xbackup = 0; xbackup < breite - 1; xbackup++)
		{
			for (x = xbackup, y = ybackup; x > 0 && y < hoehe - 1 && (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
				{
					if (nline.getlength() == searchedlength)
						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
				}
			}
		}
	}
	if (nline.getlength() == searchedlength)
		vec.push_back(nline);
	return vec;
}

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 (short h = hoehe - 1; h >= 0; h--){
		for (short 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;
}

short Spielfeld::getchainlength()
{
	return chainlength;
}

short Spielfeld::winner()
{
	vector<vector<line>> result = row(getchainlength());
	if (result.size() != 0)
	{
		if (stoppen == true)
			setactive(false);
		if (ausgabe)
			cout << "GAME ENDED -> row from "; result[0][0].line::output(); cout << endl;
		return 1;
	}
	return 0;
}

Code:
//line.cpp
#include "stdafx.h"
#include <iostream>
#include "line.h"
#include "point.h"
#include <stdio.h> 

using namespace::std;

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;
}
void line::output()
{
	cout << "Punkt eins: " << start().x() << "/" << start().y() << " Punkt zwei: " << end().x() << "/" << end().y() << " length: " << m_length << endl;
}

short line::getlength()
{
	return m_length;
}

void line::set(point a, point b)
{
	m_start = a;
	m_end = b;
	m_length = (m_start.x() == m_end.x()) ? (m_end.y() - m_start.y()) : (m_end.x() - m_start.x());
	m_length++;
}

point line::end()
{
	return m_end;
}
point line::start()
{
	return m_start;
}

Zusammenfassung, aktueller stand:
Das Spielfeld funktioniert, spielen gegen sich selbst oder eine random ziehende KI möglich.
Eine Methode wurde implementiert, welche alle Reihen aus Steinen von einem Spieler mit einer bestimmten länge erkennt und abspeichert. Diese wird genutzt um zu erkennen, ob vier gleichfarbige Steine in einer Reihe liegen.

Nächste ziele:
Die KI dazu bringen (dreier) reihen zu erweitern.
Die neue Methode könnte dazu genutzt werden, dem Spielfeld zwei zusätzliche vektoren zu geben, welche alle Reihen der Spieler abspeichern. Da muss ich mir aber nochmal gedanken drüber machen.
Ferner soll die win funktion entsprechend überarbeitet werden, sodass nur noch die Steine in der Umgebung des zuletzt eingeworfenen analysiert werden, um eine viererkette zu erkennen.
 
Zuletzt bearbeitet:
ZuseZ3 schrieb:
So, jetzt mal der Reihe nach.
Dekar->1) So sehen eigentlich meine gesamten Programme aus, das einzige was mir bisher bei den Programmen von anderen Leuten aufgefallen ist, ist die { bei der Methodendeklaration in die nächste Zeile zu schreiben. Ansonsten hab ich mir halt bei manchen if abfragen die geschweifte Klammer gespart und das return statement direkt dahinter geschrieben, was ich jetzt mal geändert habe.

Edit: siehe Beitrag unten, mein Browser hat die Einrückung nicht angezeigt :)

Du musst unbedingt deinen Code einrücken! Ich weiß nicht, welche Programme du bisher gesehen hast, aber ich habe noch keinen C++-Code gesehen, bei dem nicht eingerückt wurde. Du hast doch Python benutzt? Da wirst du sogar zum Einrücken gezwungen. Deswegen verstehe ich nicht, warum du hier deinen Code überhaupt nicht einrückst. Das ist kein guter Stil, da die Übersichtlichkeit komplett verloren geht. Das gilt übrigens auch für Java.

Welchen Editor benutzt du eigentlich? Ein guter Editor würde dir den Code automatisch einrücken.

Als Einstieg kannst du dir mal die typischen Einrückungsstile hier anschauen: http://de.wikipedia.org/wiki/Einrückungsstil#Bekannte_Einr.C3.BCckungsstile
 
Zuletzt bearbeitet:
Oh, Entschuldigung! Anscheinend liegt es an meinem Browser (Chromium unter Linux), dass die Tabs nicht angezeigt werden. Mit Firefox werden sie mir angezeigt. Ich nehme also alles zurück, die Einrückung ist so erstmal in Ordnung. :)
 
ZuseZ3 schrieb:
Edit: So, ich setzte jetzt durchgängig auf shorts, da ich mir keinen Fall vorstellen kann, wo ein Overflow möglich ist.

Was ist der Hauptgrund für die allermeisten kritischen Bugs? Dass der Entwickler sagt: "Also mehr als das kommt doch bestüüümmmt nicht"
 
Jap. Wenn schon, dann lieber durchgängig auf int. Das sollte immer der Default sein, es sei denn, es gibt einen triftigen Grund, was anderes zu verwenden. Und ich denke, du hast im Moment keinen, oder?
 
Zurück
Oben