implemented min-max algorithm